dcsimg
Results 1 to 6 of 6

Thread: Merging/Combining multiple Documents

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    4

    Question Merging/Combining multiple Documents

    Hello all,
    First time coding in VBA and I'm working on automating a script to combine multiple versions of the same Word document with comments. The only difference in the documents is the comments. The script I found and was using bellow simply inserts a new page with each successive documents content. I was looking to use the default combine function in Word, which is Merge I believe in VBA, but it didn't like my modified code. Below is the unmodified version. Any help would be greatly appreciated.

    Code:
    Sub MergeMultiDocsIntoOne()
      Dim dlgFile As FileDialog
      Dim nTotalFiles As Integer
      Dim nEachSelectedFile As Integer
    
      Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)
     
      With dlgFile
        .AllowMultiSelect = True
        If .Show <> -1 Then
          Exit Sub
        Else
          nTotalFiles = .SelectedItems.Count
        End If
      End With
     
      For nEachSelectedFile = 1 To nTotalFiles
        Selection.InsertFile dlgFile.SelectedItems.Item(nEachSelectedFile)
        If nEachSelectedFile < nTotalFiles Then
          Selection.InsertBreak Type:=wdPageBreak
        Else
          If nEachSelectedFile = nTotalFiles Then
            Exit Sub
          End If
        End If
      Next nEachSelectedFile
    End Sub

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    4

    Re: Merging/Combining multiple Documents

    This is a recorded version of the function that I need help looping.
    Code:
    ' Combines two Documents
    '
        Application.MergeDocuments OriginalDocument:=Documents("Testing 1"), _
            RevisedDocument:=Documents("Testing 2"), Destination:= _
            wdCompareDestinationNew, Granularity:=wdGranularityWordLevel, _
            CompareFormatting:=False, CompareCaseChanges:=False, CompareWhitespace:= _
            False, CompareTables:=False, CompareHeaders:=False, CompareFootnotes:= _
            False, CompareTextboxes:=False, CompareFields:=False, CompareComments:= _
            True, CompareMoves:=False, OriginalAuthor:="", RevisedAuthor:="", _
            FormatFrom:=wdMergeFormatFromPrompt

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,852

    Re: Merging/Combining multiple Documents

    but it didn't like my modified code
    please explain, error? wrong result? nothing at all?

    i tested the code with a selection of documents, it appeared to work as the code indicated it should, of course my documents were not revisions so there were no comments to compare


    if you need to use the method as posted in the second post, i would think it should be like
    Code:
     Set orig = ThisDocument
      Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)
      With dlgFile
        .AllowMultiSelect = True
        If .Show <> -1 Then
          Exit Sub
        Else
          nTotalFiles = .SelectedItems.Count
        End If
      End With
      For nEachSelectedFile = 1 To nTotalFiles
      Set doc = Documents.Open(dlgFile.SelectedItems(nEachSelectedFile))
      Application.MergeDocuments OriginalDocument:=orig, _
            RevisedDocument:=doc, Destination:= _
            wdCompareDestinationOriginal, Granularity:=wdGranularityWordLevel, _
            CompareFormatting:=False, CompareCaseChanges:=False, CompareWhitespace:= _
            False, CompareTables:=False, CompareHeaders:=False, CompareFootnotes:= _
            False, CompareTextboxes:=False, CompareFields:=False, CompareComments:= _
            True, CompareMoves:=False, OriginalAuthor:="", RevisedAuthor:="", _
            FormatFrom:=wdMergeFormatFromPrompt
      doc.Close False
    '    End If
      Next nEachSelectedFile
    this is tested to run without error, but i feel it is likely that at the end only the last document will be compared with the combination of the previous documents

    i am not sure how you would make it so that all documents are compared
    my best guess is that you should run multiple compares against the original document then insert all the compares into a single document
    probably you should test with manual compares to find what is the best result, before making the code

    :edit: just for clarification, if the code is not in the original document, then you should set orig to the correct document, rather than thisdocument
    also it may be necessary to save orig between each merge
    Last edited by westconn1; Apr 25th, 2020 at 05:52 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    4

    Re: Merging/Combining multiple Documents

    Thanks for the help. I was calling the documents incorrectly in the
    Code:
    OriginalDocument:=orig, _
            RevisedDocument:=doc,
    part of the code. I will take your advice and see about inserting a save into each part of the loop.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    4

    Re: Merging/Combining multiple Documents

    I've encountered a bug that I don't know how to fix. When I ran your code below, it didn't create a new document. When I then tired to open one, the code seemed to have worked. However, now every time I open a new word document, it has the combined files. Is that because we labeled orig as ThisDocument?
    Code:
    Sub Combine_Docs()
    '
    ' Combine_Docs Macro
    '
    '
     Set orig = ThisDocument
      Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)
      With dlgFile
        .AllowMultiSelect = True
        If .Show <> -1 Then
          Exit Sub
        Else
          nTotalFiles = .SelectedItems.Count
        End If
      End With
      For nEachSelectedFile = 1 To nTotalFiles
      Set doc = Documents.Open(dlgFile.SelectedItems(nEachSelectedFile))
      Application.MergeDocuments OriginalDocument:=orig, _
            RevisedDocument:=doc, Destination:= _
            wdCompareDestinationOriginal, Granularity:=wdGranularityWordLevel, _
            CompareFormatting:=False, CompareCaseChanges:=False, CompareWhitespace:= _
            False, CompareTables:=False, CompareHeaders:=False, CompareFootnotes:= _
            False, CompareTextboxes:=False, CompareFields:=False, CompareComments:= _
            True, CompareMoves:=False, OriginalAuthor:="", RevisedAuthor:="", _
            FormatFrom:=wdMergeFormatFromPrompt
      doc.Close False
      Next nEachSelectedFile
      
    End Sub

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,852

    Re: Merging/Combining multiple Documents

    Is that because we labeled orig as ThisDocument?
    thisdocument is always the document containing the code modules for the running code, see the clarification in my previous post

    the only reason i can see why that problem should exist is that you have your code is in the normal template, in which case the combined document would presumably be saved into thisdocument, being the normal template, so that each new document based on the normal template would then contain the combined document, i would always avoid saving the code into the normal template, but that seems to be the default when recording macros
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width