c# - 在 Word (VSTO) 中并排比较两个 RTF 文档

标签 c# .net ms-word vsto rtf

对于我的 VSTO Word 解决方案,我需要以编程方式并排“比较”两个文档。换句话说,我需要从代码中执行与单击 View > Show Side by Side 按钮等效的操作。

我尝试使用 CompareSideBySideWith加载两个文档后的方法。抛出异常:“请求的集合成员不存在”。我不是第一个遇到这种情况的人。请参阅 Microsoft 在 this thread 中的回复(样板,不是特别有用) . MS 代表最终挠着头放弃了。

我什至尝试打开两个空白文档并进行比较。这次也不异常(exception),但没有进行比较,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 block 之外尝试了它。

下面的 Per Otaku 我尝试改为调用 doc2.Windows.Compare(ref doc1),这适用于空白文档以及从 Word 2007 保存为 .docx 和 .rtf 的测试文档。

但是,我们需要比较从另一个 RTF 编辑器保存为 RTF 格式的文档。当我加载我们的一份文件时,它失败了。要重现我的错误,请尝试加载从写字板保存的 RTF 文档——这些文档也会失败。我尝试修改 Documents.Open() 的编码和格式参数,但无济于事。最好避免将临时文件转换并保存为 .docx,尤其是对于较大的文档!另请注意,我可以在手动打开写字板保存的 RTF 文件后单击“并排查看”,它可以正常工作。

此外,比较文档(作为参数传递给 Windows.CompareSideBySideWith() 的文档)的格式似乎很重要。例如,如果我们像 Otaku 那样执行 doc2.Windows.CompareSideBySideWith(ref doc1)例如,当 doc1 是常规 docx 时它起作用,但当它是从写字板保存的 RTF 时不起作用。(不管 doc2 来自哪里)。


更新 2: 照例一行代码解决好几天的追尾:

doc1.Convert(); // Updates the document to the newest object model (i.e. DOCX)

现在可以毫无问题地并排比较。

最佳答案

反转文档的比较,应该没问题:

对于新文档

Document doc1 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Document doc2 = this.word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
object o = doc1;
doc2.Windows.CompareSideBySideWith(ref o);

对于现有文档

object missing = System.Reflection.Missing.Value;
object newFilename1 = "C:\\Test\\Test1.docx";
Document doc1 = this.word.Documents.Open(ref newFilename1, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

object newFilename2 = "C:\\Test\\Test2.docx";
Document doc2 = this.word.Documents.Open(ref newFilename2, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
object o = doc1;
doc2.Windows.CompareSideBySideWith(ref o);

如果您的应用程序不可见或您正在启动 Word 的新实例,您应该设置 this.word.Visible = true; 在以 CompareSideBySideWith< 运行文档打开之前 是一个 UI 例程。

关于c# - 在 Word (VSTO) 中并排比较两个 RTF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4962280/

相关文章:

c# - 更改 ListBox 的 UniformGrid 列数的最佳方法是什么?

c# - SMTP 中继 - 从控制台应用程序发送电子邮件

vb.net - SaveAs2 For Word 2010 无法与装有 Word 2007 的客户端 PC 配合使用

c# - 带有 FlagsAttributes 的枚举

javascript - 使用javascript在word中的现有内容控件之后添加内容控件?

vba - 从第二个宏中卸载用户窗体

C#错误处理问题

c# - 使用 ASP.NET 和 Azure 创建云存储应用程序

c# - 根据答案更改表单颜色

.net - 具有多个 DataLoadOptions 的 Linq2SQl 预加载