1
votes

I have a requirement for comparing two word documents and show the differences. and i am trying for this.

I got some code by searching:

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.Visible = false;
    wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
    object wordTrue = (object)true;
    object wordFalse = (object)false;
    object fileToOpen = @"D:\doc test files\doc1.docx";
    object missing = Type.Missing;
    Microsoft.Office.Interop.Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
       ref missing, ref wordFalse, ref wordFalse, ref missing,
       ref missing, ref missing, ref missing, ref missing,
       ref missing, ref missing, ref wordTrue, ref missing,
       ref missing, ref missing, ref missing);

    object fileToOpen1 = @"D:\doc test files\doc2.docx";
    Microsoft.Office.Interop.Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
        ref missing, ref wordFalse, ref wordFalse, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing);

    Microsoft.Office.Interop.Word.Document doc = wordApp.CompareDocuments(doc1, doc2, WdCompareDestination.wdCompareDestinationNew, WdGranularity.wdGranularityWordLevel,
        true, true, true, true, true, true, true, true, true, true, "", false);
    doc1.Close(ref missing,ref missing,ref missing);
    doc2.Close(ref missing, ref missing, ref missing);

So when i run this code there comes a message while opening the doc1 and doc2

doc1.docx is locked for editing by xxxxx

and options available:

  1. Open a read only copy
  2. create a local copy and merge your changes later
  3. receive notification when the original copy is available.

I need to avoid these messages. can anybody having idea on this?

1

1 Answers

0
votes

You can use option 2 and create a copy of the document. Since you only want to know the differences and don't intend to modify the documents itself this should work.

But be aware of possible problems with multi threading in your web application. Don't just copy the file to a temp directory. In the case of user1 want to check test.docx and user2 has also a test.docx they will overwrite test.docx and you are at the same problem again. To avid this you can rename your local copy with an unique name like a GUID.