2 Ways to Quickly Merge Multiple Word Documents into One via VBA

In this article, we are glad to share with you 2 ways to quickly merge multiple Word documents into one via VBA.

In Word, there is the default built-in feature to help user combine or merge several documents into one. Details are explained in this article: How to Combine & Merge Multiple Word Documents into OneMerge Multiple Word Documents into One

With that method, you have to make a couple of clicks and the contents of next document always come right after that of the previous one. Therefore, we want to offer you quicker ways to do so.

Method 1: Merge Selected Documents into One

  1. First and foremost, create a new blank document.
  2. Trigger VBA editor in Word by pressing “Alt+ F11”.
  3. Next click “Normal”.
  4. Then click “Insert”.
  5. And choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  6. Double click on new module to open the editing space.
  7. Paste the following macro there:
Sub MergeMultiDocsIntoOne()
  Dim dlgFile As FileDialog
  Dim nTotalFiles As Integer
  Dim nEachSelectedFile As Integer

  Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)
 
  With dlgFile
    .AllowMultiSelect = True
    If .Show <> -1 Then
      Exit Sub
    Else
      nTotalFiles = .SelectedItems.Count
    End If
  End With
 
  For nEachSelectedFile = 1 To nTotalFiles
    Selection.InsertFile dlgFile.SelectedItems.Item(nEachSelectedFile)
    If nEachSelectedFile < nTotalFiles Then
      Selection.InsertBreak Type:=wdPageBreak
    Else
      If nEachSelectedFile = nTotalFiles Then
        Exit Sub
      End If
    End If
  Next nEachSelectedFile
End Sub
  1. Next click “Run” or hit “F5”.Paste Macro->Click "Run"
  2. Now in “Browse” window, select multiple files by pressing “Ctrl” and click “OK”.Select Files->Click "OK"
  3. Then all contents in selected files will be pieced together in the new document, with texts of each document starting on a new page.

Note:

As we mentioned, this macro puts texts of each document on a new page. But if you need them to be one after another, you can alter some code lines in the macro. Just find line “If nEachSelectedFile < nTotalFiles Then”, the delete it and the next six lines as well.

Method 2: Merge All Documents in a Folder into One

In case you have a folder of files to be combined, then this macro shall work beautifully.

  1. To start off, install and run macro as described in method 1.
  2. Then replace macro with this one:
Sub MergeFilesInAFolderIntoOneDoc()
  Dim dlgFile As FileDialog
  Dim objDoc As Document, objNewDoc As Document
  Dim StrFolder As String, strFile As String
 
  Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)
 
  With dlgFile
    If .Show = -1 Then
      StrFolder = .SelectedItems(1) & "\"
    Else
      MsgBox ("No folder is selected!")
      Exit Sub
    End If
  End With
 
  strFile = Dir(StrFolder & "*.docx", vbNormal)
  Set objNewDoc = Documents.Add
 
  While strFile <> ""
    Set objDoc = Documents.Open(FileName:=StrFolder & strFile)
    objDoc.Range.Copy
    objNewDoc.Activate
    With Selection
      .Paste
      .InsertBreak Type:=wdPageBreak
      .Collapse wdCollapseEnd
    End With
 
    objDoc.Close SaveChanges:=wdDoNotSaveChanges
 
    strFile = Dir()
  Wend
 
  objNewDoc.Activate
  Selection.EndKey Unit:=wdStory
  Selection.Delete
End Sub
  1. Run the macro by clicking “Run”.
  2. Next you see “Browse” window open. Select the folder where you store all documents. And click “OK”.

Similarly, texts of each document starts on a new page. To make them stick together one after another, find and delete code line “.InsertBreak Type:=wdPageBreak”.

Protect Your Word Document Properly

One of the best ways to protect your Word documents is to back them up on a regular basis. With backups in hand, you will never have to fear the loss of data. Besides, it’s also advisable to get a tool to repair docx if any of them shall be damaged.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including damaged Excel and pdf repair software products. For more information visit www.datanumen.com

9 responses to “2 Ways to Quickly Merge Multiple Word Documents into One via VBA”

  1. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  2. the script works well. just wonder how to maintain the same header and footers as the original documents?
    many thanks

  3. @Adam should be an easy fix. Just add a field to the top of each document that resets the sequential (either manually or automatically right around the .paste command) and then after the very last newDoc.activate, insert a line to refresh all fields.

    I haven‘t programmed VBA in a bit, but with some stackoverflow I‘m sure you‘ll find the commanda for it.

  4. I’m wondering if there’s a way to merge multiple files into one, but start lists in every file back at 1? When I do this, the second document merged continues the first document’s list numbering and I want each inserted file to start back at 1.

  5. This worked once for me and now everytime I run it the files are coming in for a second and third time. is there something else that we can use

Leave a Reply

Your email address will not be published. Required fields are marked *