XYplorer: Trigger Word Document Compare

A lot of my work revolves around documentationm and in business, Microsoft Word documents still rule. Word of course comes with it’s own built-in version tracking feature, but by default this feature is enabled. It has to be pueposefully enabled as it can make documents confusing and cumbersome for those who don’t fully understand how to use it. Unfortunately from time to time, people send me document revisions where they have neglected to enable track changes, and it is important to identify what has actually changed. In these scenarios, we can fall back to Word’s document compare feature to identify the changes.

Normally, you would opn Word, navigate to each fle in turn and trigger the compare. This is all fine and good, but as someone who is usually working with the files directly in XYplorer (my Windows file manager of choice), I figured it was more efficient to trigger it from within the app.

This solution consists of two parts. The first part if an XYplorer script that gets the file paths for the selected files and then passes that on to the second part. That second part is unsurprisingly a VBScript, as VBScript has good support for interacting directly with instances of Microsoft Word.

XYplorer Script - Trigger

I have this script assigned to a menu item on my Office File Tools button in XYplorer. It is just a one-liner and contains no vaidation or error handling. It simply assumes that you have a couple of Word documents selected. To date, the combination of how frequently I use the feature and how often I get the selection wrong (never so far) hasn’t necessitated me adding any additional checks.

The script runs a cscript command to run a VBScript called wordcmpr.vbs, located in the same scripts directory as my xyplorer script files. I’m only currently using this script from XYplorer, so it made sense to store it there rather than in my central VBScript directory structure.

The script is passed two file paths. These correspond to the path to the selected file in the active pane and the path to the selected file in the inactive pane. Therefore, it is essential that you have dual pane browsing enabled and a single file selected in each pane.

"Compare Word Documents";
	run "cscript ""<xyscripts>\wordcmpr.vbs"" """ . get("SelectedItemsPathNames", , a) . """ """ . get("SelectedItemsPathNames", , i) . """";

VBScript - Compare

The VBScript creates a Word object instance and opens each of the documents into it. Once loaded, the script runs the comparison, and then closes the original two documents to leave only the compare document.

Option Explicit

'Check we have a couple of parameters
If WScript.Arguments.Count = 0  Then
	WScript.Echo "Sorry, for the compare to work you must pass in two Word document file paths."
	WScript.Echo "Press [ENTER] to continue..."
	WScript.StdIn.ReadLine
Else
	'Initialise
	WScript.Echo "Initialising..."
	Dim objWord, objWordComp, objDoc1, objDoc2
	Set objWord = WScript.CreateObject("Word.Application","objword_")

	'When run from the command line we have to open the documents before we can compare them as we have to reference the objects not the paths
	WScript.Echo "Retrieving first document..."
	Set objDoc1 = objword.Documents.Open(WScript.Arguments.Item(0), , true)
	WScript.Echo "Retrieving second document..."
	Set objDoc2 = objword.Documents.Open(WScript.Arguments.Item(1), , true)

	' Compare the documents
	WScript.Echo "Running comparison..."
	Set objWordComp = objWord.CompareDocuments(objDoc1, objDoc2)

	'Now close the original docs
	WScript.Echo "Tidying..."
	objDoc1.Close
	objDoc2.Close
	Set objDoc1 = nothing
	Set objDoc2 = nothing

	'Display
	WScript.Echo "Show comparison."
	objWord.Visible = true
End If

It’s as straight forward as that, and when fighting with sets of documents where instructions around tracking changes have been overlooked or where it was not envisaged as being required, this makes carrying out the comparisons a little less painful for me. Hopefully it’ll be helpful for you too.

Author: Stephen Millard
Tags: | xyplorer |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read