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
![](https://static.wixstatic.com/media/af7fa4_9e31bd8bc1924d24b23b71a4f16d6e97~mv2.png/v1/fill/w_49,h_25,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/af7fa4_9e31bd8bc1924d24b23b71a4f16d6e97~mv2.png)
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.
![](https://static.wixstatic.com/media/af7fa4_b8ad756fd74d47c4b29a1cfd430fba5a~mv2.png/v1/fill/w_49,h_33,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/af7fa4_b8ad756fd74d47c4b29a1cfd430fba5a~mv2.png)
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