如何使用 C# 迭代和计算 Word 文档中的修订版本? [英] How to iterate and count Revisions in a Word document using C#?

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

问题描述

我一直在寻找这个问题,但在任何地方都找不到答案,所以希望这里的人可以提供帮助.

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

我正在用 C# 做一个 WinForms 应用程序,我使用 WordApplcation.CompareDocuments 来比较两个文档并获得一个结果文档,其中的更改标记为修订版.

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.

所以下一步是计算有多少单词被修改——特别是 wdRevisionDelete 和 wdRevisonInsert.

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

唯一的问题是最终的.修订有时是空的或包含大量数据(超过 500 字).

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

我在 MSDN 页面上阅读了 Revisions.Count 该文档.Revisions 不会显示所有修订,而只会显示主要故事的修订,并且我必须使用范围 - 但这没有帮助.

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.

这是我当前的代码:

using Word = Microsoft.Office.Interop.Word;

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 是 WordApplication.CompareDocuments 创建的 Word 文档

final is the Word Document created by WordApplication.CompareDocuments

所以,正如我所说的,根据 MSDN,我使用 range.Revision 而不是 document.Revision,并逐节进行.

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

只有一个包含六次修订的文档没有显示,而其他文档显示了 100 个.

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.

我已在 Word 2007 中打开了 CompareDocuments 创建的文档,并且修订已正确标记,并且可以在 Word 中接受或拒绝

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?

我注意到一些奇怪的事情 - 当我尝试将报告 0 更改的原始文档文件另存为 txt 文件时,尽管 CompareDocuments 标记(正确)一些,但我注意到并非全部页面被保存到 txt 文件 - 包括所有有修订的区域.

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.

我尝试使用 Word 2007 和 LibreOffice 3.3 转换为 txt 文件 - 两者都有相同的结果(缺少大量文本).

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

可能以某种方式相关.

想知道这个文件有什么问题.

Wonder what is wrong with this files.

有什么想法吗?

推荐答案

好吧,显然该代码没有任何问题,并且可以处理更简单的文件.

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.

就像我的编辑所说,甚至无法将它们正确保存为 txt 文件.

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

谁知道是什么原因造成的,请告诉我,同时这个是作为Word文档文件问题解决的.

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

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

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