【问题标题】:Comparing two RTF documents side-by-side in Word (VSTO)在 Word (VSTO) 中并排比较两个 RTF 文档
【发布时间】:2011-02-10 20:28:26
【问题描述】:

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

我在加载两个文档后尝试使用CompareSideBySideWith 方法。抛出异常:“集合的请求成员不存在”。我不是第一个遇到这种情况的人;请参阅this thread 中的 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() 排列窗口。这部分是因为我们的功能区包含 View Side by Side 按钮的别名,除非成功调用实际的 Side by Side 命令,否则不会打开(按下)该别名。


更新:在上面涉及两个新文档的示例中仍然抛出异常; Word 吞下了异常,因为我在 try-catch 块之外尝试了它。

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

但是,我们需要比较从另一个 RTF 编辑器保存为 RTF 的文档。当我加载我们的一份文件时,它失败了。要重现我的错误,请尝试加载从写字板保存的 RTF 文档——这些也会失败。我尝试修改 Documents.Open() 的 Encoding 和 Format 参数,但无济于事。避免将临时文件转换并保存为 .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)

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

【问题讨论】:

  • 您是否尝试先激活 doc1?喜欢:doc1.Activate。然后做 CompareSideBySideWith()...
  • 为什么 doc2object 而不是 Document
  • @Otaku:这是 Word 对象模型中的一个特性,您必须在许多参数中使用 ref 对象。
  • 明白了。我曾经使用过不需要这种东西的 VB.NET。请参阅下面的答案。

标签: c# .net ms-word vsto rtf


【解决方案1】:

颠倒你的文件的比较,应该没问题:

对于新文件

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 例程。

【讨论】:

  • 唉...这对新的空白文档很有效,但是对我通过 Documents.Open() 加载的文档应用相同的策略仍然会引发异常!我可以手动单击“并排查看”命令,没有问题,但尝试以编程方式进行并b​​oom
  • @System.Cats.Lol:对不起,你以为你只想要.Add。上面的代码适用于.Add.Open
  • @Otaku 感谢您的更新——它帮助我缩小了问题的范围——比较不喜欢从写字板(或我们自己的 RTF 编辑器)保存的 RTF 文档。请参阅对原始问题的更新。
  • @System.Cats.Lol:使用 .RTF、.ODT 或任何其他非本地格式,您现在正在通过编程超越 Word 自动化领域。对象模型是为其原生格式创建的。虽然有些事情可以用其他格式完成,但并非所有内容都可供他们使用。如果您打开两个 .HTML 文件并尝试执行 CompareSideBySideWith,您会发现同样的事情。如果以上内容对于回答您关于 Word 格式的原始问题没有用处,请告诉我,我会继续删除它。
  • @Otaku 命令将手动工作是不一致的(例如,单击“并排查看”和两个 RTF 文档),但等效的 API 命令 CompareSideBySideWith() 不起作用。要么两者都应该工作,要么都不工作。我很抱歉没有在我原来的问题中指定 RTF。您帮助我缩小了问题的范围,并且使用您的解决方案,我可以将文档就地转换为 DOCX,比较它​​们没有问题。它仍然是 Word API 中不一致的一种解决方法,但我怀疑是否有办法让本机 RTF 进行比较。答案被接受为“不可能”:-)
猜你喜欢
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 2016-02-03
  • 1970-01-01
相关资源
最近更新 更多