locked
Compare Two word Documents and save the compared report c# RRS feed

  • Question

  •             try
    
    {
    object missing = System.Reflection.Missing.Value;

    //create a readonly variable of object type and assign it to false.

    object readonlyobj = false ;

    object filename = @"C:\DOCFOLDER\Document1.doc" ;

    //create a word application object for processing the word file.

    Word.Application app = new Word.ApplicationClass();

    //create a word document object and open the above file..
    Word.Document doc = app.Documents.Open(

    ref filename, ref missing, ref readonlyobj, ref missing, ref missing,

    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    string filenm = @"C:\Document1.doc" ;

    string nm = @"C:\DOCFOLDER\Document2.doc" ;

    object filesavename = nm;

    doc.SaveAs(ref filesavename, ref missing, ref missing, ref missing, ref missing, ref missing,

    ref missing, ref missing, ref missing, ref missing, ref missing);

    doc.TrackRevisions = true ;

    doc.ShowRevisions = true ;

    doc.PrintRevisions = true ;

    doc.Compare(filenm);

    doc.Close(ref missing, ref missing, ref missing);

    app.Quit(ref missing, ref missing, ref missing);

    Above code compares two word document Document1 & Document2 .But the thing is I'm unable to save the Compared report of these two documents .when the  app.Quit(ref missing,ref missing,ref missing) line executed
    It prompts for a save dialog box which is nothing but Compared report of these two documents after i click on the Save button it asks for the path to save the file .I want these things to  be done programmatically can anyone modify the above coding so that i can save the compared report of these two word documents in particular folder eg C:\COMPARED DOC REPORTS\

    Thank you,
    Regards,
    Rahul.
    Saturday, December 5, 2009 4:04 AM

Answers

  • Hello Rahul,

    For Compare method, please refer to this MSDN article: Document.Compare Method. The third parameter of this method has three values. For this scenario, we could choose wdCompareTargetNew value to save the comparison result to a new document. After executing this method, this new document will open. We need to save it manually.

    Code like this,
                object comparetarget = Word.WdCompareTarget.wdCompareTargetNew;
                doc.Compare(@"C:\Temp\11.docx", ref missing, ref comparetarget, ref missing, ref missing, ref missing, ref missing, ref missing);
                
    If you have any further question, please feel free to follow up.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bessie Zhao Friday, December 11, 2009 11:27 AM
    Thursday, December 10, 2009 10:26 AM

All replies

  • Hello Rahul,

    For Compare method, please refer to this MSDN article: Document.Compare Method. The third parameter of this method has three values. For this scenario, we could choose wdCompareTargetNew value to save the comparison result to a new document. After executing this method, this new document will open. We need to save it manually.

    Code like this,
                object comparetarget = Word.WdCompareTarget.wdCompareTargetNew;
                doc.Compare(@"C:\Temp\11.docx", ref missing, ref comparetarget, ref missing, ref missing, ref missing, ref missing, ref missing);
                
    If you have any further question, please feel free to follow up.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bessie Zhao Friday, December 11, 2009 11:27 AM
    Thursday, December 10, 2009 10:26 AM
  • Hi All,

    Is it possible to customize the output being written to Report ?.
    example - Suppose if the user deletes more than 2 lines in a paragraph I would like to strike the whole paragraph in the report and add a new paragraph.

    Is it possible to achieve customization like this using interop? If not, how can else can it be done? I have been trying this using OOXML + Linq to XML. If this can be done with Interop things willbe lot easier.

    Thanks,
    Sandeep

    Updated


    Does anyone know what logic is used when comparing documents. The same text in different documents is giving me different result when compared.


    Ex1:

    Document1 text : This is sample paragraph

    Document2 text : This is paragraph sample

    Result: This is <del datetime="2010-03-15T12:02" cite="mailto:sandeep_s">sample </del>paragraph<ins datetime="2010-03-15T12:02" cite="mailto:sandeep_s"> sample</ins>

     

    Ex2:

     

    Document1 text : This is paragraph sample

    Document2 text : This is sample paragraph

    Result: This is <ins datetime="2010-03-15T15:08" cite="mailto:sandeep_s">sample </ins>paragraph<del datetime="2010-03-15T15:08" cite="mailto:sandeep_s"> sample</del>

    Tuesday, February 16, 2010 3:05 PM
  • It's been a long since this was answer but maybe my work around for the save issue help someone.

    As Bessie said if you use Word.WdCompareTarget.wdCompareTargetNew you have to save the resulting document manually, instead if you code like this:

    object comparetarget = Word.WdCompareTarget.wdCompareTargetCurrent;  //

                doc.Compare(@"C:\Temp\11.docx", ref missing, ref comparetarget, ref missing, ref missing, ref missing, ref missing, ref missing);

    object SaveToFormat = Word.WdSaveFormat.wdFormatXML;


                doc.SaveAs("p003.xml", ref SaveToFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    like this you place the revisions on the current document, but then you save it as a new one

    Wednesday, April 23, 2014 3:58 PM
  • Of course you can also do this outside of C#.

    http://office.microsoft.com/en-us/word-help/compare-documents-side-by-side-HP001047321.aspx

    See this too.

    http://download.cnet.com/Beyond-Compare/3000-2242_4-10015731.html


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.


    • Edited by ryguy72 Tuesday, May 6, 2014 3:32 PM
    Tuesday, May 6, 2014 3:31 PM