Search code examples
c#countms-wordinteroprevisions

How to iterate and count Revisions in a Word document using C#?


I have been looking around for this but couldn't find an answer anywhere, so hope aomeone here can help.

I am doing a WinForms application in C# in wich i use WordApplcation.CompareDocuments to compare two documents and get a result document with the changes marked as Revisions.

This works well and apart from the revisions hiding stuff inside textboxes (wich i don't care about just yet) i get exactly what i want.

So next step is to count how many words got revised - specifically wdRevisionDelete and wdRevisonInsert.

Only problem is final.Revisions is sometimes empty or contains enourmous amounts of data (over 500 words).

i read on the MSDN page for Revisions.Count that document.Revisions won't show all revisions but only ones on main story and that i must use range - but that didn't help.

here's 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 the Word Document created by WordApplication.CompareDocuments

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

Only one document with half a dozen revisions shows none while others show 100's.

So my question is, how to use the Revisions to count added / deleted words.

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

Any ideias on what i might be overlooking?

EDIT: I have noticed something odd - when i try to save as txt file the original doc files that are reporting 0 changes although the CompareDocuments marks (correctly) a few, i notice that not all pages get saved to the txt file - that includes all areas with revisions.

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

Might be related somehow.

Wonder what is wrong with this files.

Any ideas?


Solution

  • Well Apparently there's nothing wrong with that code and works on simpler files.

    There's just something odd with the files i was testing on.

    Like my edit says, can't even save them as txt files properly.

    Anyone knowing what could cause this let me know, meanwhile this one is solved as Word document file problems.