在 Word 中并排比较两个 RTF 文档 (VSTO)
对于我的 VSTO Word 解决方案,我需要以编程方式并排“比较”两个文档。换句话说,我需要从代码中执行相当于单击“View >”的操作。显示并排按钮。
我在加载两个后尝试使用 CompareSideBySideWith 方法文件。抛出异常:“请求的集合成员不存在”。我不是第一个遇到这种情况的人;请参阅 Microsoft 在 此帖子。 MS 代表最终摸不着头脑并放弃了。
我什至尝试打开两个空白文档并进行比较。这次也不例外,但比较没有发生,CompareSideBySideWith() 返回 false。
Document doc1 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
object doc2 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
doc1.Windows.CompareSideBySideWith(ref doc2);
有没有人发现这个问题的解决方法?在自定义解决方案中,这似乎是一个非常基本的功能。
注意:我们需要调用实际的“并排”比较,而不仅仅是通过 Windows.Arrange() 排列窗口。部分原因是我们的功能区包含“并排查看”按钮的别名,除非成功调用实际的“并排”命令,否则该按钮不会打开(按下)。
更新:上面的例子涉及两个新文档,仍然抛出异常; Word 吞掉了异常,因为我在 try-catch 块之外尝试了它。
根据下面的 Otaku,我尝试调用 doc2.Windows.Compare(ref doc1),这适用于空白文档以及从 Word 2007 保存为 .docx 和 .rtf 的测试文档。
但是,我们需要比较从 Word 2007 保存为 RTF 的文档另一个 RTF 编辑器。当我加载我们的一份文档时,它失败了。要重现我的错误,请尝试加载从写字板保存的 RTF 文档 - 这些也失败。我尝试修改 Documents.Open() 的 Encoding 和 Format 参数,但无济于事。最好避免将临时文件转换并保存为 .docx,特别是对于较大的文档!另请注意,我可以在手动打开写字板保存的 RTF 文件后单击“并排查看”,并且它可以工作。
另外,比较文档(作为参数传递给 Windows.CompareSideBySideWith() 的文档)的格式似乎只重要。例如,如果我们像 Otaku 的示例中那样执行 doc2.Windows.CompareSideBySideWith(ref doc1) ,那么它 但当它是从 WordPad 保存的 RTF 时则无效(无论 doc2 来自何处)。
当 doc1 是常规 docx 时有效, 像往常一样,一行代码解决了几天的追尾问题:
doc1.Convert(); // Updates the document to the newest object model (i.e. DOCX)
现在可以毫无问题地并排比较。
For my VSTO Word solution, I need to programatically "compare" two documents side-by-side. In other words I need to, from code, perform the equivalent of clicking the View > Show Side by Side button.
I tried using the CompareSideBySideWith method after loading two documents. An exception is thrown: "The requested member of the collection does not exist". I am not the first to encounter this; see Microsoft's (boilerplate, not particularly helpful) replies in this thread. The MS rep ended up scratching her head and giving up.
I even tried opening two blank documents and comparing them. This time no exception, but the compare didn't happen and CompareSideBySideWith() returned false.
Document doc1 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
object doc2 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
doc1.Windows.CompareSideBySideWith(ref doc2);
Has anyone discovered a workaround for this? It seem a pretty basic piece of functionality to have a in a custom solution.
Note: We need to call the actual "Side by Side" compare, not just arrange the windows via Windows.Arrange(). This is partly because our ribbon contains an alias for the View Side by Side button, which won't be turned on (pressed in) unless the actual Side by Side command is called successfully.
Update: The exception was still thrown in the above example involving two new documents; Word swallowed the exception because I tried it outside of my try-catch block.
Per Otaku below I tried calling doc2.Windows.Compare(ref doc1) instead, and this worked for blank documents as well as test documents saved as .docx and .rtf from Word 2007.
However, we need to compare documents saved as RTF from another RTF editor. When I load one of our documents, it fails. To reproduce my error, try loading RTF documents saved from WordPad--these fail as well. I've tried tinkering with the Encoding and Format parameters of Documents.Open() to no avail. It would be nice to avoid having to convert and save the temp file as .docx, particularly for larger documents! Also note that I can click View Side by Side after opening the WordPad-saved RTF files manually, and it works.
Also, it only seems to matter what format the compare document (the document being passed as parameter to Windows.CompareSideBySideWith() is in. For example, if we are doing doc2.Windows.CompareSideBySideWith(ref doc1) as in Otaku's example, it works when doc1 is a regular docx but not when it's an RTF saved from WordPad. (Regardless of where doc2 came from).
Update 2:
As usual, one line of code resolves several days of chasing one's tail:
doc1.Convert(); // Updates the document to the newest object model (i.e. DOCX)
Can now compare side-by-side without a problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反向比较您的文档,应该没问题:
对于新文档
对于现有文档
如果您的应用程序不可见或者您正在启动 Word 的新实例,则应该设置
this.word.Visible = true;< /code> 在运行打开文档之前,
CompareSideBySideWith
是一个 UI 例程。Reverse the compares of your documents and it should be fine:
For new documents
For existing documents
If your app isn't visible or you are launching a new instance of Word, you should set
this.word.Visible = true;
before running the opening of documents asCompareSideBySideWith
is a UI routine.