Macro to merge Word files
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


Sean O'Shea has more than 20 years of experience in the litigation support field with major law firms in New York and San Francisco.   He is an ACEDS Certified eDiscovery Specialist and a Relativity Certified Administrator.

The views expressed in this blog are those of the owner and do not reflect the views or opinions of the owner’s employer.

If you have a question or comment about this blog, please make a submission using the form to the right. 

Your details were sent successfully!

© 2015 by Sean O'Shea . Proudly created with Wix.com

bottom of page