使用Microsoft.Office.Interop.Word启用内置Microsoft Word按钮控件

时间:2013-08-02 14:53:40

标签: c# ms-word vsto ms-office

我目前需要将两个Microsoft Word文档与Microsoft.Office.Interop.Word进行比较。我找到了Application.CompareDocument方法,它完全符合我的要求。以下C#源代码(代码段)将文件系统中保存的文档与当前活动文档进行比较,并在新文档中打开结果:

using Word = Microsoft.Office.Interop.Word;

// [...]

Word.Document originalDocument = this.application.Documents.Open(filePath, ReadOnly: true, Visible: false);
Word.Document diffDocument = this.application.CompareDocuments(
    originalDocument,
    this.application.ActiveDocument);
((Word._Document)originalDocument).Close(SaveChanges: false);

// TODO Activate two built-in Microsoft Word buttons.

// [...]

但是我还需要在新创建的Word文档的视图中激活两个内置按钮。在MSDN上搜索了一段时间后,我无法找到实现我想要的方法。我在这个问题上添加了两个屏幕截图,显示了我要激活的内置按钮(遗憾的是,我使用的是德语版的Microsoft Word 2010,所以我不知道确切的翻译是什么)。

  1. “Quelldokumente anzeigen”(可翻译为“显示源文档”)。我需要激活按钮“Beide anzeigen”(可以翻译为“显示两者”)。 Screenshot of the button Quelldokumente anzeigen

  2. “Überarbeitungsbereich”(可翻译为“修订版”)。我需要激活“Überarbeitungsbereichvertikal...”按钮(可翻译为“垂直修订窗格......”)。 Screenshot of the button Überarbeitungsbereich

  3. 总而言之,我想知道如何修改这两个按钮的状态(直接或间接通过方法调用)。

    编辑(2013-08-03)

    可以通过以下方法设置修订窗格:

    diffDocument.ActiveWindow.View.SplitSpecial = Word.WdSpecialPane.wdPaneRevisionsVert;
    

    我仍在寻找显示源文档和修订文档窗格的解决方案。

    编辑(2013-08-05)

    可以修改show source documents按钮以通过以下方法显示两个源文档:

    diffDocument.ActiveWindow.ShowSourceDocuments = Word.WdShowSourceDocuments.wdShowSourceDocumentsBoth;
    

1 个答案:

答案 0 :(得分:2)

问题的可能解决方案:

<击> 广告1。但首先,您需要使用ReadOnly: false打开文档,而根据C#和VBA测试,当您将参数设置为ReadOnly: true时,它将无效:< / strike>

<击>     ((Word._Document)diffDocument).Windows.CompareSideBySideWith(originalDocument);

广告2。这次你需要引用Word应用程序的窗口对象。这是活动窗口的代码:

appWRD.ActiveWindow.View.SplitSpecial = Word.WdSpecialPane.wdPaneRevisionsVert;

其中:appWRD在我的代码中为Word.Application

广告1再次。(上面的一个是误解的结果)。

根据一些测试,此代码应该为您提供所需的信息:

appWRD.ActiveWindow.ShowSourceDocuments = Word.WdShowSourceDocuments.wdShowSourceDocumentsBoth;