I need to compare two texts and I am using MS Word using the following script:
dim ORIGINAL
dim REVISED
dim WORD_COMPARE
dim objWord
Set objWord = CreateObject("Word.Application")
objWord.DisplayAlerts = WdAlertsNone
objWord.Visible = True
set ORIGINAL = objWord.Documents.Open(WScript.Arguments.Unnamed.Item(0), False, True, False) ' ORIGINAL DOCX
Set REVISED = objWord.Documents.Open(WScript.Arguments.Unnamed.Item(1), False, False, False) ' MODIFIED.DOCX
dim wordCompareResultFilePath:wordCompareResultFilePath = WScript.Arguments.Item(3) ' FILE RESULT PATH
Set WORD_COMPARE =_
objWord.CompareDocuments(_
ORIGINAL,_
REVISED,_
1,_
1,_
False,_
False,_
true,_
False,_
False,_
True,_
False,_
False,_
False,_
True, _
"COMPARER_USER",_
False)
dim printLines:printLines=""
for each revision in REVISED.Revisions
'printLines = printLines & original.range(revision.range.paragraphs.item(1).range.start).paragraphs.item(1).range.text & vbCrLf ' GET THE ORIGINAL PARAGRAPH
printLines = printLines & revision.range.paragraphs.item(1).range.start & vbCrLf
next
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 'text
stream.Position = 0
stream.Charset = "utf-8"
stream.WriteText printLines
stream.SaveToFile wordCompareResultFilePath, 2
stream.Close
ORIGINAL.Close False
REVISED.Close False
objWord.Quit
WScript.Quit Err.Number
To execute it I use the following .bat file in a cmd:
cscript.exe "C:\Users\x\Desktop\comparer\comparer_step1.vbs" "C:\Users\x\Desktop\comparer\original.docx" "C:\Users\x\Desktop\comparer\modificado12.docx" "C:\Users\AdminRPozuelo\Desktop\comparer\87a0ba51-d28e-48b1-8c93-c57e276b72d3.docx")
I use this to perform the comparison and get for example, the index of the revision in order to use in the original to get the original text(the foreach line in the script).As far as I know Ms Word returns two revisions for a simple replace an insert, and a deletion(or deletion and insert,depending of how the parameters are specified). When I execute this in my local machine works ok and I get the following indexes for a simple change (a replace), what as far as I understand is okay because the index should be the same:
However, when I use this in other machines, returns different result even with the same MS Word version(msword 2019), for example in a Microsoft Server with MSWord 2016 the same change are returning the following indexes:
Hence my question: why this line produces different indexes in different machines for the same text:
revision.range.paragraphs.item(1).range.start
I suspect I need to specify more parameters in the comparison. Does anybody can help me?
Thank you very much in advance.
P.S. I apologize for my English, Shakespare must be rolling over in his grave at the moment.
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
If you deal with open XML documents only you may consider using the Open XML SDK instead, see Welcome to the Open XML SDK 2.5 for Office for more information. Or just consider using third-party components designed for the server-side execution.