如何遍历和计数使用C#在Word文档中的修订? [英] How to iterate and count Revisions in a Word document using C#?

查看:839
本文介绍了如何遍历和计数使用C#在Word文档中的修订?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处寻找这一点,但到处都找不到答案,所以在这里希望aomeone能有所帮助。



我做在C#中的WinForms应用程序至极我使用WordApplcation.CompareDocuments来比较两个文件,​​并得到与标记为修订变化的结果文档。



这工作得很好,除了从修订躲在里面的文本框的东西( 。我至极不关心,只是还没有)我得到我想要的东西。



那么下一步就是要统计有多少的话得到了修改 - 特别wdRevisionDelete和wdRevisonInsert <。 / p>

唯一的问题是final.Revisions有时是空或包含enourmous的数据(超过500字)。



我读了MSDN页上Revisions.Count该document.Revisions不会显示在主要故事的所有修订版,但唯一的,我必须使用范围 - 但这并没有帮助



这是我当前的代码:

 使用Word =的Microsoft.Office.Interop.Word; 

 的foreach(Word.Section S IN final.Sections)
{
的foreach(在s.Range.Revisions Word.Revision R)
{
计数器+ = R .Range.Words.Count;
如果(r.Type == Word.WdRevisionType.wdRevisionDelete)
delcnt + = r.Range.Words.Count;
如果(r.Type == Word.WdRevisionType.wdRevisionInsert)
inscnt + = r.Range.Words.Count;
}
}



最后是WordApplication.CompareDocuments创建的Word文档



所以,正如我所说的,并根据MSDN,我使用range.Revision代替document.Revision,去逐节。



只有一个文档半打修订显示无,而其他显示100的。



所以我的问题是,如何使用修订数增加/删除的文字。



我已经打开了CompareDocuments创建在Word 2007文档和修订被正确标记,并可以接受或内部字

$ b拒绝
$ b

这是什么,我可能俯瞰任何ideias



编辑:我注意到一个奇怪的现象 - 当我尝试保存为txt文件都报告0的变化虽然CompareDocuments标记(正确地)原数DOC文件,我注意到,并非所有的网页获得保存到txt文件 - 包括与修订各领域



我试过同时使用的Word 2007和LibreOffice 3.3转换为txt文件 - 都具有相同的结果(文字丢失的很多的)



可能以某种方式有关。



神奇什么是错的这个文件。



任何想法?


解决方案

那么显然没有什么错码,适用于简单的文件。



这里还有一些奇怪的与我在测试上的文件。



就像我的编辑说,甚至不能将它们保存为正常的txt文件。



任何人都知道这是什么原因让我知道,同时这个人是解决为Word文档文件的问题。


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?

解决方案

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.

这篇关于如何遍历和计数使用C#在Word文档中的修订?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆