locked
Compare Word Documents c# RRS feed

  • Question

  •  

    Hi All,

     

    I have to compare the text of two word documents using C#. These word documents contain images and text also. But I just wanted to compare the text.

     

    Can anybody please let me know how to do this?

     

    Thanks,

    Ravi Mullick

    Friday, September 14, 2007 5:15 AM

Answers

  •  

    object missing = System.Reflection.Missing.Value;

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

    object readonlyobj = false;

    object filename = txtfile1.Text;

    //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 = txtfile2.Text;

    string nm = txtpath.Text + @"\" + getfilename(txtfile1.Text);

    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);

    MessageBox.Show("Process complete");

    Monday, September 17, 2007 4:12 AM

All replies

  • I think that you must use Visual Studio Tools for Office.

    Regards.
    Friday, September 14, 2007 5:58 AM
  • Friday, September 14, 2007 6:01 AM
  •  

    Hi friend

     

    Thanks for ur reply.I just want to compare the text of two word documents and create a new document in

    which i know the difference in red color along with the data.

    Thanks & regards

     

    Friday, September 14, 2007 11:47 AM
  • See the simplest example. For VBA

    Code Snippet

    For a = 1 To Documents("doc1.doc").Words.Count


        Documents("doc1.doc").Words(a).Copy
        Documents("doc3.doc").Words(a).Paste
          
        If Documents("doc1.doc").Words(a).Text <> Chr(13) _
                And Documents("doc1.doc").Words(a).Text <> Chr(1) Then
            If Documents("doc1.doc").Words(a).Text <> Documents("doc2.doc").Words(a).Text Then _
                Documents("doc3.doc").Words(a).Font.Color = wdColorRed
        End If


    Next

     

     

    Saturday, September 15, 2007 5:32 AM
  •  

    object missing = System.Reflection.Missing.Value;

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

    object readonlyobj = false;

    object filename = txtfile1.Text;

    //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 = txtfile2.Text;

    string nm = txtpath.Text + @"\" + getfilename(txtfile1.Text);

    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);

    MessageBox.Show("Process complete");

    Monday, September 17, 2007 4:12 AM
  • @ Ravi_asp.net the above code works perfectly .But how could I save the compared report .i.e I want to save the Final compared document report .can u modify the above coding as i couldn't solve myself  :(
    Saturday, December 5, 2009 4:02 AM
  • Hi,

    I have used the below code and it is working for me for saving the compared document.

        public class WordDocComparer
        {
            #region Variables
            private Microsoft.Office.Interop.Word.Application WordApp;
            private Document aDoc;
            string _sourceFilePath;
            string _destinationFilePath;
            #endregion

            #region Constructor
            /// <summary>
            /// This is the constructor for WordDocComparer
            /// </summary>
            public WordDocComparer(string source, string destination)
            {
                _sourceFilePath = source;
                _destinationFilePath = destination;
            }
            #endregion

            #region Public Methods
            public void Compare()
            {
                object missing = System.Reflection.Missing.Value;

                object readonlyobj = false;
                object filename = _sourceFilePath;

                //create a word application object for processing the word file.
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

                //create a word document object and open the above file..
                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);

                doc.TrackRevisions = true;
                doc.ShowRevisions = true;
                doc.PrintRevisions = true;
                doc.Compare(_destinationFilePath, missing, WdCompareTarget.wdCompareTargetNew, true, false, false, false, false);

                doc.Close(ref missing, ref missing, ref missing);
                app.Quit(ref missing, ref missing, ref missing);
                MessageBox.Show("Process complete");
            }
            #endregion

            #region Private Method
            #endregion
        }

    Thanks and Regards.

    Ankush Asthana

    Sunday, June 10, 2012 8:30 AM
  • hello Ravi sir

    i want to save my compare result of two word document in my root as other new doc file  and also i want to handle mistake list or mistake count through C# 

    please suggest me how can i do...

    i am waiting for you reply..

    Thursday, February 4, 2016 10:07 AM