Reputation: 878
i want to compare 2 word documents For example my first document contains text
"The quick brown fox jumps over the lazy dog" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is commonly used for touch-typing practice. It is also used to test typewriters and computer keyboards, show fonts, and other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known.
and my 2nd document may have some changes in words or sentences for example 2nd document contains text like this.
"This text should be highlighted" "The quick brown fox jumps over the lazy dog" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is commonly used for touch-typing practice. "This text should be highlighted" It is also used to test typewriters and computer keyboards, show fonts, and other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known. "This text should be highlighted"
Now i've changed text in 2nd document document and i want to highlight these changes in 2nd document not in 3rd document, because i'm creating versions of documents 1st document will be the 1st version and 2nd document will be the 2nd version for user to download.
Please help me i want to use Microsoft.Office.Interop.Word.
Upvotes: 0
Views: 4347
Reputation: 878
This works for me i hope this will help for others as well..
string fileToOpen = @"d:\doc1.docx";
string fileToCompare = @"d:\doc2.docx";
Application app = new Application();
object miss = System.Reflection.Missing.Value;
object readOnly = true;
object AddToRecent = false;
object Visible = false;
var doc = app.Documents.Open(fileToOpen, ref miss, readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
doc.TrackRevisions = true;
doc.ShowRevisions = false;
doc.PrintRevisions = true;
doc.Compare(fileToCompare, miss, WdCompareTarget.wdCompareTargetCurrent, true, false, false, false, false);
object SaveToFormat = WdSaveFormat.wdFormatDocumentDefault;
string outputFileName = @"d:\output.docx";
doc.SaveAs2(outputFileName, SaveToFormat, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
doc.Close();
var word = app.Application;
word.Quit();
Marshal.ReleaseComObject(doc)
Marshal.ReleaseComObject(word);
Marshal.ReleaseComObject(app);
doc = null;
word = null;
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Upvotes: 0
Reputation: 25673
Word has the Compare functionality - you'll find it in the Reviewing tab of the Ribbon, towards the right, in its own group. That lets you specify two documents and compare the content. Differences are displayed as "track changes" (reviewing) and you can specify in which document to show them, or to show them in a new document.
Try that in the Word application and if it does what you need, you can use the Word.Application.CompareDocuments method to make the comparison using the "interop".
Upvotes: 0