Comparing Word Documents in Team Foundation Server

May 19, 2009 23:28 by admin

We’re thinking about putting some of our Microsoft Word documents under source control in our Team Foundation Server. It would be great to be able to compare MS Word documents in the same way we compare source code.

The Team Foundation Server client can be configured to use any executable to compare or merge two files. In Visual Studio 2005, for example, go to Tools / Options then select the Source Control section. On the Visual Studio Team Foundation Server panel press the “Configure User Tools…” In the Configure User Tools dialog you can associate different file extensions with any comparison or merge tools you want.

For instance I changed my default comparison tool to KDiff3 by associating .* with the KDiff3.exe file. Then for comparisons I set the command line to:

%1 --fname %6 %2 --fname %7

and for merge:

%3 --fname %8 %2 --fname %7 %1 --fname %6 -o %4

Now KDiff3 does a great job with code, but comparing MS Word documents is not its forte! Looking around online, many people recommend Diff Doc. I hooked it up and it worked pretty well… but I wasn’t quite satisfied. Microsoft Word 2007 can compare two documents… surely it can do the comparison for me?

Looking at the command-line switches for Word 2007 there isn’t one that instructs it to compare two files, but there is one that allows you to run a macro… and comparing two documents is possible from a macro!

So I recorded a macro to compare two documents, then adapted it for my purposes. Here is an early version that probably needs more work:

Sub CompareDocuments()

    Application.CompareDocuments(OriginalDocument:=Documents(1), _

        RevisedDocument:=Documents(2), 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 _


    ActiveWindow.ShowSourceDocuments = wdShowSourceDocumentsNone



End Sub

This macro compares the first two open documents it finds, initialising some comparison settings. This will create a new document containing the track changes between the two source documents. It then closes the two source documents.

If this macro is saved into your template, it can then be called by the TFS client. Associate WinWord.exe with .doc and .docx then set the command line to:

/mCompareDocuments /n /q %2 %1

/mCompareDocuments calls the above macro. %2 %1 pass the document filenames. /n starts a new instance of MS Word, which should mean only the two documents to be compared are available in the Documents collection (I couldn’t find a way for the macro to directly use the filenames passed on the command line). /q suppresses the splash screen.

Give it a go… record your own macro to try different settings.

kick it on


Comments (4) -

March 31. 2010 09:11

Munish Malik

Hi Piers,
Thanks to google, I landed up on this page. And the more I read your profile, the more it impresses me regarding your exposure to technologies.
I work on a CRM solution. And currently I am involved in designing a module that takes input as an excel, converts it into a word document. And stores it at some central location accessed by all users. (I still have to figure out a way to store these documents in unix and if not then in NT).
After that I want the users to have the ability to choose any two documents from the GUI application to compare. Once the user selects two rows (representing the two word documents), I need to trigger the Word Comparison feature, that will open MS word 2007 and compare the two documents.
I came to know that macros would be the best way to implement this, but my inexperience with macros, and VBA is not helping me.
I see the above macro that you wrote, and I am yet to try it out but your requirement seems to be similar to mine.
Were you able to design this macro that takes input as two word documents, and then compares it via MS Word 2007.


Munish Malik

March 31. 2010 22:34


This macro does the comparison, however, automating it as part of a web service may be a challenge as you will need to run up MS Office to do the work. If you can get past that problem, perhaps the easiest way to get started on your macro is to record it. Start the recording feature, manually perfu.orm the work you need to do then stop recording.... then look at the code that has been generated. I hope it goes well for yo


January 18. 2011 02:04

testosteron kaufen

automating is everything imo

testosteron kaufen

May 5. 2011 12:54


Thanks for your post,

I too changed the default comparison tool of VS2010, and used SVN as a comparison tool, and still use TFS as a source code repository.

As a result, no Macro to write, just use it straight after.


Add comment

  Country flag

  • Comment
  • Preview