Share via

Batch compare word documents

Anonymous
2013-05-10T12:54:41+00:00

I need to find the differences between two versions of several MS word documents. The files are organized as follows:

Version1Folder

  1. Folder1
  • File1.doc
  • File2.doc
  • File3.doc
  • .....
  • .....
  • Filex.doc
  1. Folder2
  • File21.doc
  • File22.doc
  • File23.doc
  • .....
  • .....
  • File2x.doc

Version2Folder

  1. Folder1
  • File1.doc
  • File2.doc
  • File3.doc
  • .....
  • .....
  • Filex.doc
  1. Folder2
  • File21.doc
  • File22.doc
  • File23.doc
  • .....
  • .....
  • File2x.doc

I must compare File1.doc in Version1Folder against File1.doc in Version2Folder. I need to compare all x number of files. The results may be saved in a new folder or even in Version2Folder .

I use Windows XP and Office 2007. I can do it for individual files using Compare Document option in the Review tabof MS word. But that doesnt solve the problem as the requirement comes frequently for various versions.

Is it possible using Macro or VBscript? I tried recording a macro but was not able to execute it for files with various names/folder structure. Please help.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 322.9K Reputation points MVP Volunteer Moderator
    2013-05-13T07:38:47+00:00

    Sorry, that line of code should have been

    MkDir fldrVersion1 & "\Compared"

    Also, the following line of code

     ActiveDocument.SaveAs2 fldrVersion1 & "Compared" & docVersion1.Name

    should be

     ActiveDocument.SaveAs2 fldrVersion1 & "\Compared" & docVersion1.Name

    It will probably be best to delete the folder that you manually created.

    1 person found this answer helpful.
    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 322.9K Reputation points MVP Volunteer Moderator
    2013-05-11T03:51:18+00:00

    Try the following code:

    Dim fldrVersion1 As String, fldrVersion2 As String

    Dim strVersion1 As String, strVersion2 As String

    Dim docVersion1 As Document, docVersion2 As Document

    Dim docCompareTarget As Document

    Dim fd As FileDialog

    Set fd = Application.FileDialog(msoFileDialogFolderPicker)

    With fd

        .Title = "Select the folder that contains the original files."

        If .Show = -1 Then

            fldrVersion1 = .SelectedItems(1)

        Else

            MsgBox "You did not select a folder."

            Exit Sub

        End If

    End With

    With fd

        .Title = "Select the folder that contains the revised files."

        If .Show = -1 Then

            fldrVersion2 = .SelectedItems(1)

        Else

            MsgBox "You did not select a folder."

            Exit Sub

        End If

    End With

    For i = 1 To 2

        fldrVersion1 = fldrVersion1 & "\Folder" & i & ""

        fldrVersion2 = fldrVersion2 & "\Folder" & i & ""

        MkDir fldrVersion1 & "Compared"

        strVersion1 = Dir$(fldrVersion1 & "*.doc*")

        While strVersion1 <> ""

            Set docVersion1 = Documents.Open(strfldrVersion1 & strVersion1)

            Set docVersion2 = Documents.Open(strfldrVersion1 & docVersion1.Name)

            docVersion1.Compare Name:=docVersion2, compareTarget:=wdCompareTargetNew

            ActiveDocument.SaveAs2 fldrVersion1 & "Compared" & docVersion1.Name

            ActiveDocument.Close

            docVersion1.Close wdDoNotSaveChanges

            docVersion2.Close wdDoNotSaveChanges

            strVersion1 = Dir$()

        Wend

    Next i

    It should create new folders with the name Compared under Version1Folder\Folder1 and Version1Folder\Folder2 in which it will save the documents created by the comparisons.

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2013-05-13T07:02:47+00:00

    Hi Doug,

    Thanks for the update. When I run this script as a macro, I'm able to select the version1 and version2 folders. However, soon after i select the second folder, it shows the following error:

    Run-time error '75':

    Path not found

    When I click 'Debug', the following code is highlighted.

    MkDir fldrVersion1 & "Compared"

    The same error occurs even when I create a folder by name "Compared" inside Version1Folder\Folder1.

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 322.9K Reputation points MVP Volunteer Moderator
    2013-05-13T06:05:19+00:00

    See the article "What do I do with macros sent to me by other newsgroup readers to help me out?” at:

    http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm

    0 comments No comments
  5. Anonymous
    2013-05-13T05:18:29+00:00

    Thanks Doug, 

    Is this code supposed to be run as a VBS? I tried saving it as a vbs file and ran it but got an error:

    Line: 1

    Char: 18

    Error: Expected end of statement

    Code: 800A0401

    Could you please guide me with some more details on how to execute it. Thank you.

    0 comments No comments