Comparing two word documents RRS feed

  • Question

  • Hi,

    I am trying two compare two different versions of word document. I am using Visual Studios 2010 (VB) and there I am using the .Compare method and can see the difference between the two. But I want to create a text (.txt) file which will contain only the summaries of the compare result. I looked into the following url -

    But it does not show me how to just get the summary of the compare. I believe that if I can see the summary of the compare in "Revision" tab, somehow I will be able to extract that particular data via a VB code also. But as of now, I am not getting anywhere.

    To be more specific, what I am doing is that when I am opening the file, I am saving it as - Globals.ThisAddIn.Application.ActiveDocument.SaveAs("File1.doc", Word.WdSaveFormat.wdFormatDocumentDefault)

    Then I am making the changes in the file and again saving the new version as - Globals.ThisAddIn.Application.ActiveDocument.SaveAs("File2.doc", Word.WdSaveFormat.wdFormatDocumentDefault)

    Now, I want to create a separate document(or text file) which will show me the compare result between these two files. I even tried using .DocumentCompare() method ... but seem to have some problem with it.

    Can anyone give me some inputs on how shall I proceed.

    Thanks in advance,


    Friday, March 18, 2011 6:26 AM

All replies

  • Hi AritraDattaGupta,

    Thanks for posting in the MSDN Forum.

    It bases on my experience that we’re able to save the result as a word document to show where is different. And if you want to save as a txt file, you’re able to do that but you will get the document which be compared and lost all of the details of compare result. So, I recommend you save the result as a word document.

    This is the snippet I can provide. I developed it via Visual Studio 2010 & Word 2010 due to I have no environment to reproduce your issue under Visual Studio 2008 & Word 2003.

    Dim app As Word.Application
        Dim doc As Word.Document
        Dim doc1 As Word.Document
        Dim doc2 As Word.Document
          OpenFileDialog1.Filter = "Word Document(*.docx)|*.docx"
          app = Globals.ThisAddIn.Application
          doc = app.ActiveDocument
          doc1 = app.Documents.Open(OpenFileDialog1.FileName)
          doc2 = app.Documents(Index:="Compare Result 1")
          doc2.SaveAs2(FileName:="[you save path]result.docx", FileFormat:=Word.WdSaveFormat.wdFormatDocument)
        Catch ex As Exception
          MsgBox(ex.Message, , "Exception")
        End Try

    Have a good day,


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 22, 2011 3:25 AM
  • Hi Tom,

    First and foremost thank you for providing an insight on how to solve the scenarion. And sorry for being so late in the reply, I was busy with other projects and couldnot look into this. Unfortunately writing the code in the above mentioned way is not working in my case. I guess, I need to tell the process in a bit more details.

    Actually I am opening a word document and editing the same but saving it in a different name. While saving, I want to save the new version along with the compare file.

    First and foremost when I am opening the document via Document_Open() method, I am inserted the following code -

    Globals.ThisAddIn.Application.ActiveDocument.SaveAs("X.doc", Word.WdSaveFormat.wdFormatDocumentDefault)

     --- by this I am assuring that after opening the document and before editing anything, I am saving a copy.

    In the method of Document_Save() I am writing it as follows

    Dim doc As Word.Document
    Dim docCurre As Word.Document 
    Dim docPrev As Word.Document

    docCurr = Globals.ThisAddIn.Application.ActiveDocument
    docPrev = Globals.ThisAddIn.Application.Documents.Open("X.doc")
    doc = Globals.ThisAddIn.Application.Documents(Index:="Compare Result 1")
    doc.SaveAs("result.doc", FileFormat:=Word.WdSaveFormat.wdFormatDocumentDefault)

    Now the problem is that, the "result.doc" is showing the change as everything is being deleted (If I change docCurr and DocPrev then everything is coming as added). I am not understanding why it is not getting the content of the docPrev. I am missing some very easy things?

    Any help will be highly appreciated.







    Thursday, March 31, 2011 5:10 AM
  • Hello once again,

    I did the code as follows -

    Dim docCurr as Microsoft.Office.Interop.Word.Document
    Dim docPrev as Microsoft.Office.Interop.Word.Document
    Dim doc as Microsoft.Office.Interop.Word.Document

    docCurr = Globals.ThisAddIn.Application.ActiveDocument
    docCurr.SaveAs("New.doc", Word.WdSaveFormat.wdFormatDocumentDefault)
    docPrev = Globals.ThisAddIn.Application.Documents.Open("Old.doc" .... i.e the document that I have opened)
    doc = Globals.ThisAddIn.Application.CompareDocuments(docPrev, docCurr)
    doc.SaveAs("result.doc", Word.WdSaveFormat.wdFormatDocumentDefault)

    And I am getting the compared document as well as I am getting the current document.

    But what I want is the summary of the compare. i.e. no. of changes, inserts, deletes etc. Any way to do that?

    Do let me know if you find any bug in this or if I am missing something.

    Thanks for all your help.

    Thursday, March 31, 2011 8:25 AM
  • is the code working in word 2010?
    Thursday, December 8, 2011 8:45 PM
  • I try, but in word2010, the result is different than word2003 and word 2007
    Friday, January 13, 2012 3:10 PM