+ Reply to Thread
Results 1 to 19 of 19

VBA - Programmatically Finding and Appending Word Documents With Identical Names

  1. #1
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    VBA - Programmatically Finding and Appending Word Documents With Identical Names

    I am looking for a code to do a task similar to the task described in the subject. I would like to know if anyone could kindly help me with this.

    I have two folders (Letters_1 and Letters_2) each containing over 800 ".doc" files. Each ".doc" file in Letters_1 has a matching ".doc" file in Letters_2 and both have identical file names. I am looking for a VBA code that can help me automatically find and append the matching file in Letters_2 folder to the end of each corresponding ".doc" in Letters_1 folder without needing to open the files one by one.
    I'd be greatly thankful if you could please assist me with this.

  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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    For that you might use a macro like:
    Please Login or Register  to view this content.
    Change the filepaths to suit.
    Last edited by macropod; 03-07-2017 at 12:20 AM. Reason: Omitted ':' from filepaths
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Thanks Macropod. Tried it and it failed. This screenshot may help.
    Attached Images Attached Images
    Last edited by mkh862; 03-06-2017 at 10:07 PM.

  4. #4
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23
    Quote Originally Posted by mkh862 View Post
    Thanks Macropod. Tried it and it failed. This screenshot may help.
    P.s
    Folder 2 is a subset of Folder 1 i.e not every file in Folder 1 has a matching file in Folder 2.
    Last edited by mkh862; 03-06-2017 at 11:10 PM.

  5. #5
    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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    It's not apparent to my why the code would have failed - especially at that point. What error message was displayed?

  6. #6
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Quote Originally Posted by macropod View Post
    It's not apparent to my why the code would have failed - especially at that point. What error message was displayed?
    Thanks again Macropod.
    To try the code, I created two test folders: Letters_1_Folder and Letters_2_Folder, the latter being the one containing the texts that are supposed to be added to the end of the files in folder1. As you see in the screenshots, only 3 of the files in folder1 have a matching file in folder2.
    Looks like I got that {Run-time error ‘5’ | Invalid procedure call or argument} error because the paths were not assigned to Scr and Tgt folders properly.
    I copied the code in VBA where the active document was part (1) from folder 1. I also switched the paths as following:
    strFldrTgt = " C:\Merged_Docs\Letters_1_Folder "
    strFldrSrc = " C:\Merged_Docs\Letters_2_Folder "
    and it worked, but only the first pair of matching documents (part (2) files) were merged and the rest remained unchanged. I assume it is because the run stops at {strFlNm = Dir()} on line #23.
    I am wondering if you would please create a similar pair of files and folders and see whether the code could be amended so that multiple pairs of files can be merged automatically. Thank you so much in advance.

    folders.png
    Last edited by mkh862; 03-07-2017 at 04:11 PM.

  7. #7
    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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Hmm,

    It seems the second call to Dir wipes out the first. Try:
    Please Login or Register  to view this content.
    Again, change the filepaths to suit.

  8. #8
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    It's odd! Clicked on the "run" button and it does nothing!

  9. #9
    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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    It will if you changed the filepaths appropriately.

  10. #10
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Entered the paths a shown in the shots (with the "\" at the end), got "error 4605".
    1-error 4605.png
    2-vba window.png

  11. #11
    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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Apart from anything else, your filepaths are wrong. Using:
    strFldrTgt = "C:\Merged Docs\" & "\Letters_1_Folder\"
    strFldrSrc = "C:\Merged Docs\" & "\Letters_2_Folder\"
    results in paths that look like:
    C:\Merged Docs\\Letters_1_Folder\
    C:\Merged Docs\\Letters_2_Folder\
    You should use:
    strFldrTgt = "C:\Merged Docs\Letters_1_Folder\"
    strFldrSrc = "C:\Merged Docs\Letters_2_Folder\"

    I have no idea what that error message means regarding the highlighted line. Does the target document have some kind of protection applied?

  12. #12
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    No it doesn't have any protection.
    Fixed the paths, still got the same error!

  13. #13
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    When I run the code while the active document is a file in folder2 instead of one in folder1, no error occurs but only one file gets appended.

  14. #14
    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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Other than the document containing the macro, none of the documents in Letters_1_Folder or Letters_1_Folder should be open when the macro is run.

    If you're still having problems, perhaps you could attach a sample of each document to your next post.

  15. #15
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    I tried the code on another test files/folders same as the attached that contain a few lines of reiterated words, the code worked perfectly fine. But it doesn't work on my principal documents that unfortunately I am not authorized to post here.
    Do you think the existence of special characters such as section breaks, new line, asterisks ,.. at the end of the documents could cause the problem?
    After deleting some of these characters, no merge is performed, even the single one that was previously done.

    Merged_Docs.zip

  16. #16
    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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    I have just extracted the folders from your attachment (to a new folder in my Downloads folder) and run the macro against them. The documents named 'part (2).doc', 'part (4).doc' and 'part (6).doc' in the Letters_1_Folder were all updated. The others weren't updated as there were no corresponding files in the Letters_2_Folder. As I understand it, that is as it should be.

    Perhaps part of the problem with how you're trying to use the macro is that you're now trying to process files in folder attached directly to C:\ instead of in folders attached to your user account.

  17. #17
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Quote Originally Posted by macropod View Post
    I have just extracted the folders from your attachment (to a new folder in my Downloads folder) and run the macro against them. The documents named 'part (2).doc', 'part (4).doc' and 'part (6).doc' in the Letters_1_Folder were all updated. The others weren't updated as there were no corresponding files in the Letters_2_Folder. As I understand it, that is as it should be.

    Perhaps part of the problem with how you're trying to use the macro is that you're now trying to process files in folder attached directly to C:\ instead of in folders attached to your user account.
    Same problem with the folders being in users' directory. Strangely, the code on the test files/folders works sporadically!
    Do you think the read-only settings in the code might cause the issue? After the code is failed, opening the target files displays no content as if opening in read-only mode.

    Is there any way to fix the issue of the second call of Dir function in the first edition of the code you posted? Looks like the first version of the code which is pretty straightforward would not act as odd as the second one does.
    Last edited by mkh862; 03-08-2017 at 05:36 PM.

  18. #18
    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: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    The read-only setting (which applies only to the files in the Letters_2_Folder) has no effect on the outcome; as I said, the code works fine for me with your sample files. Evidently there is something wrong with your system. Perhaps an Office repair is in order (via Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change > Repair).

  19. #19
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA - Programmatically Finding and Appending Word Documents With Identical Names

    Thank you so much Macropod! Finally it worked; you rock man!

+ 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. Split a single word document into multiple word documents
    By CaptainCool in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2014, 03:31 AM
  3. Batch Compile 100+ multiple word documents into a single word document
    By JamesFrames8 in forum Word Formatting & General
    Replies: 4
    Last Post: 04-03-2014, 08:49 PM
  4. Appending worksheet to another (identical) one in Excel 2007
    By CharleyDog in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-14-2012, 08:13 PM
  5. Problems with blank box when appending text documents
    By Hjahren in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-17-2010, 08:31 AM
  6. Finding identical names across 3 spreadsheets
    By roflcopterpilot in forum Excel General
    Replies: 1
    Last Post: 04-08-2010, 05:10 PM
  7. Finding Words In Excel Spreadsheets & Word Documents
    By rbonner79416 in forum Excel General
    Replies: 0
    Last Post: 03-18-2005, 01:06 PM

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