GuidesVB Script to Compare Two Text Files

VB Script to Compare Two Text Files

Download Code For This Article

DataFast Consulting

DataFast Access Utility
Want a quick and easy way to check some class module files or SQL Script files for changes? Check out this Visual Basic Script for an automated and simplified process of comparing files.

Why Do I Need To Compare Documents?

Good question, and the answer will depend on your particular circumstances.

In my case, the need to compare documents arose when I wanted to find the differences
in various code modules. Of course, Visual Source Safe will do this for you, but what
if you just want to check some class module files … or SQL Script files for changes?
Maybe your code isn’t in VSS or you just want a fast way to verify that code modules
haven’t changed?

How can this be accomplished quickly? Enter Microsoft Word Automation

MS Word Compare Documents Feature

Perhaps you weren’t aware that Microsoft Word has a Compare Documents feature. To
activate it, open any document (we’ll refer to it as DocA) in Word. Next, go to the
Tools menu and select Compare from the Track Changes option. (See illustration)
Navigate to the second document (DocB) and click the Open button. Presto-chango,
Word displays a merged document with all differences marked in red, strike-out font.

Automating the Process

Of course, this is a somewhat tedious process that can be automated and simplified
with VB Script. You can copy and paste the following code into Notepad and save as
a .vbs file … or simply download my CompareDocs.vbs file.
Comments explaining the code are interspersed below.

' ***************************************************
'  Script To Invoke the MS Word Compare Docs
'  Danny J. Lesandrini     May 31, 2001
'  [email protected]
'  This script will uses COM Automation to access
'  the MS Office Object Model in order to use the
'  MS Word Compare Documents routine on two files.
'  The user is prompted for the paths of the two
'  documents to be compared.  The FileSystemObject
'  is used to perform validation on the file paths
'  provided by the user.  If not valid, the script
'  exits gracefully.
'  Note that the CreateObject call uses the generic
'  reference, "Word.Application" with no version
'  number.  This assures that the script won't break
'  just because the client machine doesn't contain
'  the correct version of MS Word.  The script
'  will break, however, if no version of Word is
'  found on the client machine
' ***************************************************

Dim objWord
Dim objScript
Dim strDocA
Dim strDocB
Dim fContinue
'Const vbExclamation = 48 

strDocA = InputBox("Enter complete path first doc", "Compare")
strDocB = InputBox("Enter complete path second doc", "Compare")

' Assume that user entered valid file paths
fContinue = True

' Test file paths to DocA and DocB for validity.
' -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Set objScript = CreateObject("Scripting.FileSystemObject")
fContinue = objScript.FileExists(strDocA)
fContinue = objScript.FileExists(strDocB)
Set objScript = Nothing

If fContinue = False Then
    MsgBox "Invalid File Paths" ,vbExclamation ,"Error"
    ' Only continue if user has typed text into InputBox.
    Set objWord = CreateObject("Word.Application")

    objWord.Documents.Open strDocA
    objWord.ActiveDocument.Compare strDocB
    objWord.Visible = True

    Set objWord = Nothing
End If


As you can see, it doesn’t take much code to implement this solution. You must
have MS Word installed and properly registered for it to work, but all things considered,
it’s a fast and efficient way to compare two documents.

Latest Posts

Related Stories