Monday, December 03, 2007

Comparing two Word Documents using Word VBA

Compare Word Documents using VBA

Here is a simple routine, which will compare two Microsoft Word documents and return the status.


Sub IsDocument_Equal()

Dim oDoc1 As Word.Document
Dim oResDoc As Word.Document

' Delete the tables from both the document

' Delete the images from both the document

' Replace Paragraphs etc

Set oDoc1 = ActiveDocument

' comparing Document 1 with New 1.doc
oDoc1.Compare Name:="C:\New 1.doc", CompareTarget:=wdCompareTargetNew, DetectFormatChanges:=True

'This will be the result document
Set oResDoc = ActiveDocument

If oResDoc.Revisions.Count <> 0 Then
'Some changes are done
MsgBox "There are Changes "
Else
MsgBox "No Changes"
End If

End Sub

2 comments:

  1. Anonymous10:00 AM

    how to make it user friendly like... instead of assigning path in pgm , we shall get from user

    ReplyDelete
  2. Anonymous12:09 AM

    Hi Everyone,

    I've recorded a macro that compares 2 documents but after editing the code I was prompted with an error. can anyone help me to correct this code?

    'ChangeFileOpenDirectory ""
    'ChangeFileOpenDirectory ""
    Application.CompareDocuments OriginalDocument:=Documents( _
    "C:\Users\AAA\Desktop\Transcribed\VT290115.doc"), RevisedDocument:=Documents( _
    "C:\Users\AAA\Desktop\Edited\VT290115.doc"), Destination:=wdCompareDestinationNew, Granularity _
    :=wdGranularityWordLevel, CompareFormatting:=True, CompareCaseChanges:= _
    True, CompareWhitespace:=True, CompareTables:=True, CompareHeaders:=True, _
    CompareFootnotes:=True, CompareTextboxes:=True, CompareFields:=True, _
    CompareComments:=True, CompareMoves:=True, RevisedAuthor:="Author", _
    IgnoreAllComparisonWarnings:=False

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group