Consulting

Results 1 to 10 of 10

Thread: Merge multiple documents at once

  1. #1

    Merge multiple documents at once

    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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Andy,

    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:
    [vba]
    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
    [/vba]
    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
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    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

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Andy,

    Your most welcome, glad it was what you need.
    Quote Originally Posted by neodjandre
    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:
    [vba]
    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[/vba]

    HTH
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Andy, is this one solved or do you have more questions?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6
    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

  7. #7
    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

  8. #8
    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?

  9. #9
    VBAX Newbie
    Joined
    May 2016
    Posts
    1
    Location
    Quote Originally Posted by wgmeisheid View Post
    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.

  10. #10
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •