PDA

View Full Version : Merge multiple documents at once



neodjandre
06-16-2008, 03:24 AM
Hello,

I have a directory which has several copies of the same word document. A different person has added comments in each copy and I am looking for a macro to combine these comments into one file. Essentially, repeat the command 'Compare and merge documents' legal blackline a number of times..

could anyone help please?

thanks
andy

MOS MASTER
06-16-2008, 02:44 PM
Hi Andy, :hi:

If I understand you correct you have one source document (the original) and a lot of other documents that need to be merged with this original?

And you want to do it all at ones?

If so you can do the following:
A: Open up your original document
B: Paste this code into a VBE module:

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

C: Don't forget to change the path in the code. (to where your documents are that need merging with the current document)
D: Run the code!

This will merge all word documents in the targeted folder with the current one.

HTH

neodjandre
06-17-2008, 02:05 AM
Hi MOS Master. that is what I want!

thanks a lot!

Is there a chance to let users browse for the directory containing all the files to be merged?

Andy

MOS MASTER
06-17-2008, 12:01 PM
Hi Andy,

Your most welcome, glad it was what you need.


Is there a chance to let users browse for the directory containing all the files to be merged?

Sure, there are many ways to do so I'll give you one.
Change the code you have for:

Option Explicit
Sub MergeDocuments()
Dim iFile As Integer
Dim sMergePath As String
sMergePath = MergeFolder
If sMergePath = vbNullString Then Exit Sub

With Application.FileSearch
.FileType = msoFileTypeWordDocuments
.LookIn = sMergePath
.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
Function MergeFolder() As String
MergeFolder = vbNullString
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the folder of the merge files"
If .Show = -1 Then
MergeFolder = .SelectedItems(1)
End If
End With
End Function

HTH

MOS MASTER
06-18-2008, 12:29 PM
Andy, is this one solved or do you have more questions?

neodjandre
07-10-2008, 08:07 AM
Hi Mos_Master..

Sorry for the late reply I was away on holiday.

I have tested your macro but can you amend it to close all open documents after conversion is finished and only keep open the master one?

Also I don't want format changes to be shown if that is possible.

thanks in advance,
Andy

neodjandre
07-22-2008, 04:32 AM
Mos_Master,

I have extensively tested your macro and it does not appear to work as expected.

I would like all the changes to be merged in the Activedocument at the time the macro is run.

Currently, the documents contained in the folder contain the changes...

could you help with this?

thanks
Andy

wgmeisheid
10-13-2011, 09:59 AM
Kudos. This is a very useful macro but when I tried it in Word 2007 I get a VBA error saying the Application.FileSearch command is not available on this platform.

How should I change the macro for 2007/2010?

jvedula
05-26-2016, 09:14 AM
Kudos. This is a very useful macro but when I tried it in Word 2007 I get a VBA error saying the Application.FileSearch command is not available on this platform.

How should I change the macro for 2007/2010?

I am getting an run-time error '5792'
The file appears to be corrupted.

gmayor
05-27-2016, 01:02 AM
This is a very old code from 2008 and FileSearch was removed from VBA in Word 2007. The following uses an alternative approach, and may do what you want, but try it with only a small number of documents in the folder


Option Explicit

Sub MergeDocuments()
Dim iFile As Integer
Dim sMergePath As String
Dim strFile As String
Dim i As Long
sMergePath = MergeFolder
If sMergePath = vbNullString Then Exit Sub

strFile = Dir$(sMergePath & "*.doc")
While strFile <> ""
MergeDocument sMergePath & strFile
i = i + 1
strFile = Dir$()
Wend
MsgBox ("The code finished merging: " & i & " documents")
End Sub
Sub MergeDocument(sPath As String)
Application.ScreenUpdating = False
ActiveDocument.Merge FileName:=sPath, _
MergeTarget:=wdMergeTargetSelected, DetectFormatChanges:=True, _
UseFormattingFrom:=wdFormattingFromPrompt, AddToRecentFiles:=False
End Sub
Function MergeFolder() As String
MergeFolder = vbNullString
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the folder of the merge files"
If .Show = -1 Then
MergeFolder = .SelectedItems(1) & Chr(92)
End If
End With
End Function