Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 40

Thread: Macro to merge mulitple word doc into one word doc

  1. #1
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post Macro to merge mulitple word doc into one word doc

    Hi Experts

    I am looking for a macro which will merge multiple word document placed in a folder into one document. I find the below code and it fits into my requirement. I need to amend the code to ask for the folder location instead of setting a default location to pick the word docs.

    Please let me with the amendments.

    Sub MergeDocs()
        Dim rng As Range
        Dim MainDoc As Document
        Dim strFile As String
        Const strFolder = "P:\Doc files\New folder\New folder\" 'change to suit
        Set MainDoc = Documents.Add
        strFile = Dir$(strFolder & "*.doc") ' can change to .docx
        Do Until strFile = ""
            Set rng = MainDoc.Range
            rng.Collapse wdCollapseEnd
            rng.InsertFile strFolder & strFile
            strFile = Dir$()
        Loop
        MsgBox ("Files are merged")
        
    End Sub

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,218
    Location
    Sub MergeDocs()
    Dim rng As Range
    Dim MainDoc As Document
    Dim strFile As String, strFolder As String
      With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Pick folder"
        .AllowMultiSelect = False
        If .Show Then
          strFolder = .SelectedItems(1) & Application.PathSeparator
        Els
          Exit Sub
        End If
      End With
      Set MainDoc = Documents.Add
      strFile = Dir$(strFolder & "*.doc") ' can change to .docx
      Do Until strFile = ""
        Set rng = MainDoc.Range
        rng.Collapse wdCollapseEnd
        rng.InsertFile strFolder & strFile
        strFile = Dir$()
      Loop
      MsgBox ("Files are merged")
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Greg

    Thanks for the code. It really worked.

    Sorry for troubling you little more. I find one discrepancy in my end result i.e. in most of the cases the document ends up in the next page, half section of the next page and the doc next to it starts from half way of that page. It looks messy. I think it is called page break. In case of manual intervention, we pull the new doc from the half page to the start of the next page. Is it possible to amend the code to do the same activity automatically?
    Regards,
    JD

  4. #4
    You should insert a next page section break e.g.

    Sub MergeDocs()
    Dim rng As Range
    Dim MainDoc As Document
    Dim strFile As String, strFolder As String
    Dim Count As Long
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Pick folder"
            .AllowMultiSelect = False
            If .Show Then
                strFolder = .SelectedItems(1) & Application.PathSeparator
            Else
                Exit Sub
            End If
        End With
        Set MainDoc = Documents.Add
        strFile = Dir$(strFolder & "*.doc")        ' can change to .docx
        Count = 0
        Do Until strFile = ""
            Count = Count + 1
            Set rng = MainDoc.Range
            With rng
                .Collapse wdCollapseEnd
                If Count > 1 Then
                    .InsertBreak wdSectionBreakNextPage
                    .End = MainDoc.Range.End
                    .Collapse wdCollapseEnd
                End If
                .InsertFile strFolder & strFile
            End With
            strFile = Dir$()
        Loop
        MsgBox ("Files are merged")
    lbl_Exit:
        Exit Sub
    End Sub
    You may also be interested in http://www.gmayor.com/Boiler.htm which would give you more control over what you are collating.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham
    First of all, how are you?
    I tried the above code in the macro, but the result is unchanged as in same result.
    Also, I would like to inform you that I have table in all the word docs which needs to be merged.
    I have a common word at the end of every doc file – “Contact details”. Could we set this as a criteria amd whenever this word is encountered the page should get break and the next doc should start from the new page.
    Is that possible!
    Regards,
    JD

  6. #6
    I am doing OK - I have another medical appointment next week when I may know more.

    The modified macro I posted should insert each separate document starting on a new page. It certainly does here. Is that not happening for you? The table should not affect the results, and searching for a text string is less efficient than selecting the end of the document.

    Did you replace Greg's code with the revised version?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    I wish everything should be fine with the reports.

    I did replaced the code.

    Unfortunately, the documents are getting merged and reflecting one after the other without Page break.

    Regards,
    JD

  8. #8
    Hmmm. Click the button on the home tab of the ribbon (or CTRL+Shift 8) and see if the section breaks have been inserted. I can't immediately see why the code I posted doesn't work.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post

    Hi Graham

    I did that and the result is same. Please see the screen-shot the Section break(Continuous) is appearing in the same page. I am not sure if this is what you meant.

    Regards,
    JD
    Attached Images Attached Images

  10. #10
    If those breaks were not in the original documents, it would seem that for some reason the code is inserting continuous section breaks rather than next page section breaks as instructed. If they were in the original documents, then

    Change
    wdSectionBreakNextPage
    to

    wdPageBreak
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    You are a Champ!

    This is perfect.

    Regards,
    JD

  12. #12
    I still can't for the life of me understand why the original didn't work for you. Which Word version are you using?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  13. #13
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    I am using word 2010.

    Regards,
    JD

  14. #14
    Same here
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  15. #15
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    Strange!

    Sorry for troubling you little more. Say after running the above macro I will get different word docs into one file. On the contrast if I need to split the same merged file back to individual files..Is that possible.

    Regards,
    JD

  16. #16
    There is some code at http://www.gmayor.com/individual_merge_letters.htm that will help with that.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  17. #17
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    There are so many codes available in the above link. Could you please let me know with which code should I go with.

    Regards,
    JD

  18. #18
    None of them will work without modification unless you resolve the issue of why your PC is not creating the section breaks from the first code. I can't see any valid reason why it should create a page break but not a next page section break. Resolve that and you can use the 'Splitter' macro, which I think is the first listed code.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  19. #19
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham
    I tried the code again but same result. Is there anything wrong with my Word doc setting? Please let me know if that is the case. I tried all the permutation and combination from my end to find the root cause.
    Regards,
    JD

  20. #20
    There is no reason why the following should perform any differently, but I would be interested to see if it does.

    Sub MergeDocs()
    Dim rng As Range
    Dim MainDoc As Document
    Dim strFile As String, strFolder As String
    Dim Count As Long
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Pick folder"
            .AllowMultiSelect = False
            If .Show Then
                strFolder = .SelectedItems(1) & Application.PathSeparator
            Else
                Exit Sub
            End If
        End With
        Set MainDoc = Documents.Add
        strFile = Dir$(strFolder & "*.doc")        ' can change to .docx
        Count = 0
        Do Until strFile = ""
            Count = Count + 1
            Set rng = MainDoc.Range
            With rng
                .Collapse 0
                If Count > 1 Then
                    .InsertBreak 2
                    .End = MainDoc.Range.End
                    .Collapse 0
                End If
                .InsertFile strFolder & strFile
            End With
            strFile = Dir$()
        Loop
        MsgBox ("Files are merged")
    lbl_Exit:
        Exit Sub
    End Sub
    If not, reboot the PC. Word 2010 will store up errors and the only way I have found to clear them is to reboot. Ensure there are no orphan lock files - see http://www.gmayor.com/what_to_do_when_word_crashes.htm

    Temporarily rename the normal template. If you have not changed the default location enter or copy
    %appdata%\Microsoft\Templates
    to the address window of Windows Explorer and you will be taken to the folder that contains the default template - which for Word 2010 is normal.dotm. Note that this folder is normally hidden.

    Restart Word and see if the above macro works. If it does, close Word, restore your old normal template and try it again.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

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