How to iterate and recount versions in a Word document using C #?

I searched for this, but could not find the answer anywhere, so I hope this can help.

I am making a WinForms application in C # in which I use WordApplcation.CompareDocuments to compare two documents and get the resulting document with the changes marked as "Revision".

This works well, and in addition to the corrections hiding the material inside the text fields (which I do not need yet), I get exactly what I want.

So, the next step is to count how many words have been revised - specifically wdRevisionDelete and wdRevisonInsert.

Only the problem is final. Sometimes errors are empty or contain huge amounts of data (more than 500 words).

I read on the MSDN page for Revisions.Count of this document. The revisions will not show all changes, but only those that are indicated in the main story, and that I should use the range, but this did not help.

here is my current code:

using Word = Microsoft.Office.Interop.Word; 

and

 foreach (Word.Section s in final.Sections) { foreach (Word.Revision r in s.Range.Revisions) { counter += r.Range.Words.Count; if (r.Type == Word.WdRevisionType.wdRevisionDelete) delcnt += r.Range.Words.Count; if (r.Type == Word.WdRevisionType.wdRevisionInsert) inscnt += r.Range.Words.Count; } } 

final is a Word document created by WordApplication.CompareDocuments

So, as I said, and according to MSDN, I use range.Revision instead of document.Revision and go to section by section.

Only one document with half a dozen revisions is shown, while others do not show 100.

So my question is how to use Revision to count added / deleted words.

I opened the documents that CompareDocuments creates in Word 2007, and the Editions are correctly marked and can be accepted or rejected inside Word

Any ideas on what I can ignore?

EDIT: I noticed something strange - when I try to save doc source files that report 0 changes as a txt file, although CompareDocuments notes (correctly) a few, I notice that not all pages are saved in a txt file, which includes all areas with changes.

I tried to convert to a txt file using Word 2007 and LibreOffice 3.3 - both have the same result (a lot of text is missing).

Somehow it may be connected.

Interestingly, what is wrong with these files.

Any ideas?

+4
source share
1 answer

Well, apparently, there is nothing wrong with this code and is working on simpler files.

Just something weird with the files I tested.

Like my edits, I can’t even save them as txt files.

Anyone who knows what might cause this, let me know, meanwhile this issue is being addressed as problems with Word files.

0
source

All Articles