top of page

Macro to merge Word files


Tonight, I successfully tested the below vba code, posted here by Greg Maxey. It is designed to merge multiple Word documents together.

Begin by opening a Word document, and entering Visual Basic by pressing ALT + F11. Enter this code in a new module.

You can change the extension of the Word documents referenced by the macro on this line:

strFile = Dir$(strFolder & "*.doc") ' can change to .docx

When you run the macro, it will prompt you to select a folder that contains the files you want to merge.

A new document will open which contains the contents of each source document separated by section breaks.

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


bottom of page