Share via


Using the CompareDocuments Method in Word

Office Quick Note banner

Handy Programming Tips for Microsoft Word 2010: Learn how to use the CompareDocuments method in Microsoft Word 2010.

Applies to: Office 2010 | VBA | Word 2010

In this article
Add the Code to the Visual Basic Editor
Test the Solution
Next Steps

Published:   May 2011

Provided by:    Frank Rice, Microsoft Corporation

The CompareDocuments method compares two documents and returns a Document object. That object represents a third document that contains the differences between the two compared documents, and it is marked by using Track Changes. In this topic, you programmatically change two documents, and then create a third document that shows the differences between the two compared documents. To complete this task, you must do the following:

  • Add the Code to the Visual Basic Editor

  • Test the Solution

Add the Code to the Visual Basic Editor

In this task, you add programming code that shows how to use the CompareDocuments method.

To add code to the Visual Basic Editor

  1. Start Word 2010.

  2. On the Developer tab, click Visual Basic to open the Visual Basic Editor.

    Note

    If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Custom Ribbon, select Developer, and then click OK.

  3. In the Projects pane, click ThisDocument.

  4. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Sub DemoCompareDocuments()
        ' Save the current document, including this code.
        Const path1 As String = "C:\Temp\Doc1.docm"
        Const path2 As String = "C:\Temp\Doc2.docm"
        Const path3 As String = "C:\Temp\Doc3.docm"
    
        Dim doc1 As Document
        Dim doc2 As Document
        Dim doc3 As Document
    
        ' Save with macros enabled, because this code exists within
        ' the document that you are saving. If the document did not
        ' contain code, you would not need to specify the file format.
        Set doc1 = ActiveDocument
        doc1.SaveAs path1, wdFormatXMLDocumentMacroEnabled
    
        ' Make some changes to the current document.
        Set doc2 = ActiveDocument
        doc2.ApplyQuickStyleSet2 "Elegant"
        ChangeTheDocument doc2
    
        ' Save the document as Doc2
        doc2.SaveAs2 path2, wdFormatFlatXMLMacroEnabled
    
        ' Open the original document
        Set doc1 = Documents.Open(path1)
    
        Set doc3 = Application.CompareDocuments(doc1, doc2, _
         Destination:=wdCompareDestinationNew, _
         Granularity:=wdGranularityWordLevel, _
         CompareFormatting:=True, _
         CompareCaseChanges:=True, _
         CompareWhiteSpace:=True)
    
        doc3.SaveAs2 path3, wdFormatFlatXMLMacroEnabled
    
    End Sub
    
    Private Sub ChangeTheDocument(doc As Document)
        ' Make some changes to the active document:
        ' Delete paragraph 3:
        doc.Paragraphs(3).Range.Delete
    
        ' Change a few words:
        doc.Paragraphs(1).Range.Words(3).Case = wdUpperCase
        doc.Paragraphs(2).Range.Words(6).Case = wdUpperCase
        doc.Paragraphs(3).Range.Sentences(2).Case = wdUpperCase
    
        ' Delete a few words:
        doc.Paragraphs(1).Range.Words(8).Delete
        doc.Paragraphs(1).Range.Words(12).Delete
        doc.Paragraphs(2).Range.Words(6).Delete
        doc.Paragraphs(3).Range.Words(10).Delete
    
        ' Format a few words:
        doc.Paragraphs(1).Range.Words(10).Bold = True
        doc.Paragraphs(2).Range.Words(5).Italic = True
        doc.Paragraphs(3).Range.Words(6).Underline = True
    End Sub
    

Test the Solution

In this task, you add text to a document. Then you step through the VBA code to see how it changes two documents and then creates a third document to compare and highlight the differences between the two compared documents.

To run the code

  1. Read through the code to form an idea of the actions is performs.

  2. In the document, type the following command (without the quotation marks). This adds five paragraphs of five sentences to the document.

    "=rand(5,5)"

  3. Drag the Visual Basic Editor window to the right side of your monitor.

  4. Drag the Word window to the left side of your monitor and adjust the windows until you can see them both.

  5. Press F8 to step through the code line-by-line and watch the document as the code progresses.

Next Steps