sullenest
03-28-2012, 01:46 PM
Hello,
Word only allows combining two documents at a time. Is there a way to use VBA to combine multiple documents (not append at the end, but combine) that are using the same template. Basically, I have created a main template where different users put in their comments. I need to merge and combine them together into one document.
I found an old code that probably worked with 2003, but after FileSearch function was removed in 2007 it doesn't work anymore. Appreciate your help very much.
Option Explicit
Sub MergeDocuments()
Dim iFile As Integer
With Application.FileSearch
.FileType = msoFileTypeWordDocuments
'change the folder to the merge documents folder
.LookIn = "C:\My documents to merge folder"
.Execute
For iFile = 1 To .FoundFiles.Count
MergeDocument (.FoundFiles(iFile))
Next
End With
If iFile <> 0 Then
MsgBox ("The code finished merging: " & CStr(iFile - 1) & " documents")
End If
End Sub
Sub MergeDocument(sPath As String)
Application.ScreenUpdating = False
ActiveDocument.Merge FileName:=sPath, _
MergeTarget:=wdMergeTargetSelected, DetectFormatChanges:=True, _
UseFormattingFrom:=wdFormattingFromPrompt, AddToRecentFiles:=False
End Sub
Word only allows combining two documents at a time. Is there a way to use VBA to combine multiple documents (not append at the end, but combine) that are using the same template. Basically, I have created a main template where different users put in their comments. I need to merge and combine them together into one document.
I found an old code that probably worked with 2003, but after FileSearch function was removed in 2007 it doesn't work anymore. Appreciate your help very much.
Option Explicit
Sub MergeDocuments()
Dim iFile As Integer
With Application.FileSearch
.FileType = msoFileTypeWordDocuments
'change the folder to the merge documents folder
.LookIn = "C:\My documents to merge folder"
.Execute
For iFile = 1 To .FoundFiles.Count
MergeDocument (.FoundFiles(iFile))
Next
End With
If iFile <> 0 Then
MsgBox ("The code finished merging: " & CStr(iFile - 1) & " documents")
End If
End Sub
Sub MergeDocument(sPath As String)
Application.ScreenUpdating = False
ActiveDocument.Merge FileName:=sPath, _
MergeTarget:=wdMergeTargetSelected, DetectFormatChanges:=True, _
UseFormattingFrom:=wdFormattingFromPrompt, AddToRecentFiles:=False
End Sub