+ Reply to Thread
Results 1 to 7 of 7

Merging two word documents using VBA

  1. #1
    Registered User
    Join Date
    09-13-2016
    Location
    Cambridge
    MS-Off Ver
    2003
    Posts
    12

    Merging two word documents using VBA

    I am a complete novice here... so any help suggested is really appreciated

    I have a managed to write a macro to loop over directory and extract table value and save to new filename which is tablevalue.doc

    Now I would like to do the following but I am struggling...

    I have a directory of word files with 99 files named company1.doc --- company99.doc - this is my current directory
    I want to loop over the each file in the current directory
    open the first word document say company1.doc
    extract the value of Table 2, cell 1 and row 2 --- this value will be either low/medium/high - this will then be the name of the word document I want to append to my current word document company1.doc
    In this case in company1.doc the value in Table 2, cell1, row 2 is low
    So now I want to go to another directory (this is path is always c:\deanj\) and contains the filenames low.doc/medium.doc/high.doc
    I want to get the contents of c:\deanj\low.doc
    I now want to append the contents of low.doc into my active word document company1.doc
    save the concatenated word document into company1.doc
    then go back through the loop

    I would like to keep the formatting from both files.

    Any help quickly will be greatly appreciated

    Thanks
    DeanJ
    Last edited by DeanJaz; 09-13-2016 at 01:30 PM.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Merging two word documents using VBA

    Based on your description, you could use a macro like:
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    09-13-2016
    Location
    Cambridge
    MS-Off Ver
    2003
    Posts
    12

    Re: Merging two word documents using VBA

    Thanks Paul. That work well apart from the format of content of low.doc/medium.do & high.doc files when appended to company1.doc..company99.doc was not correct. That's because low.doc/medium.doc/high.doc contain a combination of text/tables - it seems to have pasted the content but doesn't fit on portrait A4 page correctly within the page limits?

    Any suggestions o rectify this please
    Thanks again
    DeanJaz

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Merging two word documents using VBA

    As posted, the code assumed the last Section in your source documents would have the same page layout as your low/medium/high documents. If that's not always the case, and/or you want the additional content to start on a new page, you should perhaps consider giving your low/medium/high documents a 'Next Page' Section break at the start of each of them, perhaps with the header & footer for the new Section unlinked from the previous one.

  5. #5
    Registered User
    Join Date
    09-13-2016
    Location
    Cambridge
    MS-Off Ver
    2003
    Posts
    12

    Re: Merging two word documents using VBA

    Hi Paul

    Brilliant. Changing the format of the high/low/medium.doc worked.
    Thanks a million for the code. Really helped me out here.
    I've managed to decipher what's going on here with the code.

    One query about word VBA macros and version of word...
    I tried to run the macro using Word 2016 and it failed at the statements
    Case "low": .Range.Characters.Last.FormattedText = wdDocL.Range.FormattedText
    Case "medium": .Range.Characters.Last.FormattedText = wdDocM.Range.FormattedText
    Case "high": .Range.Characters.Last.FormattedText = wdDocH.Range.FormattedText

    Are VBA macros from word not transferable across different versions of word. I use Word 2003 at work and
    Word 2016 at home...

    Thanks again
    DeanJaz

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Merging two word documents using VBA

    The code I posted should work on all Word versions from Word 97 onwards. That said, you might try:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-13-2016
    Location
    Cambridge
    MS-Off Ver
    2003
    Posts
    12

    Re: Merging two word documents using VBA

    Thanks Paul. That worked!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Choose multiple Word-Documents via UserForm TextBox and combine them to Master Word-Doc
    By ODeveloper in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-15-2015, 09:25 AM
  2. Merging Excel Documents
    By lphan in forum Excel General
    Replies: 2
    Last Post: 02-15-2013, 02:57 PM
  3. Merging 2 active documents?
    By ILVENOFX in forum Excel General
    Replies: 4
    Last Post: 03-07-2012, 05:36 AM
  4. Merging txt documents into one document
    By Hjahren in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-20-2010, 07:35 AM
  5. Merging Documents
    By GoBuckeyes27 in forum Excel General
    Replies: 1
    Last Post: 09-18-2009, 11:32 AM
  6. merging 2 excel documents
    By canberry in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 02-06-2009, 05:43 PM
  7. Replies: 4
    Last Post: 02-06-2006, 05:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1