简体   繁体   English

我们如何使用 Open XML SDK for Office 创建 MS WORD 插件功能区

[英]How can we create a MS WORD Add-in ribbon uisng Open XML SDK for Office

In one VSTO project for MS WORD , I created a custom Office Ribbon - with a button - for WORD 2010-2016 using VS2017 - Update 1809 as follows.MS WORD一个VSTO项目中,我使用VS2017 - Update 1809WORD 2010-2016创建了一个自定义 Office 功能区 - 带有一个按钮,如下所示。 Question : How can I achieve exactly the same using Open XML SDK 2.5 for Office in a similar VS2017 - Open XML for Office project?问题:如何在类似的VS2017 - Open XML for Office项目中使用Open XML SDK 2.5 for Office实现完全相同的效果? I've not found such examples online:我在网上没有找到这样的例子:

Ribbon in VSTO example : VSTO 示例中的功能区

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns" label="my Ribbon Tab">
        <group id="ContentGroup" label="Content">  
          <button id="textButton" label="Insert Text" screentip="Text" onAction="OnTextButton"  
             supertip="Inserts text at the cursor location."/> 
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

Callback method of the button in the above ribbon :上述功能区中按钮的回调方法

public void OnTextButton(Office.IRibbonControl control)
        {
            Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
            currentRange.Text = "This text was added by the Ribbon.";
        }

UPDATE :更新

Motivation : I've created a VSTO add-in for MS WORD that creates a custom tab in WORD doc's top ribbon.动机:我为 MS WORD 创建了一个 VSTO 加载项,它在 WORD 文档的顶部功能区中创建了一个自定义选项卡。 The tab has multiple controls (buttons, checkboxes, etc.) that perform various actions via their respective callbacks (similar to the example shown in my post above).该选项卡有多个控件(按钮、复选框等),它们通过各自的回调执行各种操作(类似于我上面的帖子中显示的示例)。 I want to convert that VSTO add-in to do the same using Open XML SDK for Office because VSTO Add-ins cannot be published to Microsoft Stores as explained here .我想转换是VSTO加载项为Office做同样的使用Open XML SDK,因为VSTO外接程序不能被发布到Microsoft Stores的解释在这里 And new office Add-in functionality does not support some functionalities that my legacy VSTO add-in does.并且新的 office 插件功能不支持我的旧 VSTO 插件所支持的某些功能。

What you imagine is unfortunately not possible.不幸的是,您的想象是不可能的。

By design , Ribbon XML is linked to the code in the same "container".按照设计,Ribbon XML 链接到同一“容器”中的代码。 Ribbon XML stored in an Office document must work with call-backs in the VBA code of the same document.存储在 Office 文档中的功能区 XML必须与同一文档的 VBA 代码中的回调一起使用。 If you were to use Office Open XML to insert Ribbon XML into a Word document then the code it works with must be in a VBA component in the same document.如果要使用 Office Open XML 将 Ribbon XML 插入 Word 文档,则它使用的代码必须位于同一文档的 VBA 组件中。 (It's also worth noting that VBA components are not in XML format, but binary and therefore cannot be generated using Open XML - only imported.) (还值得注意的是,VBA 组件不是 XML 格式,而是二进制格式,因此无法使用 Open XML 生成 - 只能导入。)

By the same token, Ribbon XML loaded by a COM add-in (bases on IDTExtensibility2, whether VSTO or any other COM Add-in) must work with code in that Add-in.出于同样的原因,由 COM 加载项(基于 IDTExtensibility2,无论是 VSTO 还是任何其他 COM 加载项)加载的 Ribbon XML 必须与该加载项中的代码一起使用。 The Ribbon's call-backs cannot be in any other "container", although the call-back code can call code outside the "container". Ribbon 的回调不能在任何其他“容器”中,尽管回调代码可以调用“容器”之外的代码。 (Note that a VSTO ribbon cannot be called by VBA code... and the Ribbon is not stored in any document.) (注意 VSTO 功能区不能被 VBA 代码调用......并且功能区存储在任何文档中。)

With Ribbon XML in VBA and COM add-ins it's possible to share Ribbon controls by assigning them a Q id.使用 VBA 和 COM 加载项中的 Ribbon XML,可以通过为它们分配Q id 来共享Ribbon 控件。 For shared controls, code in any container can refer to them, as long as the id is known.对于共享控件,只要知道 id,任何容器中的代码都可以引用它们。

The Ribbons defined in Office JS Add-ins are also dependent on the code being inside the same container. Office JS 加载项中定义的功能区也依赖于同一容器内的代码。 The Office JS model does not (yet?), however, work with the concept of shared controls.但是,Office JS 模型(还没有?)支持共享控件的概念。

In conclusion: Office JS Add-ins are still very limited in comparison to the COM object models, whether this is the Office application's object model or the Ribbon ("commands" in the Office JS parlance).结论:与 COM 对象模型相比,Office JS 加载项仍然非常有限,无论是 Office 应用程序的对象模型还是功能区(Office JS 术语中的“命令”)。 The functionality is being extended steadily, recently with emphasis on Excel.该功能正在稳步扩展,最近重点是 Excel。 Other things will (hopefully) follow.其他事情(希望)会随之而来。

If there is a particular functionality that should - in your opinion - have a really high priority you should go to UserVoice for Office development.如果有一个特定的功能应该 - 在您看来 - 具有非常高的优先级,您应该转到 UserVoice 进行 Office 开发。 If the request has already been made, up-vote it.如果已经提出请求,请对其进行投票。 If you can't find it, create one.如果找不到,请创建一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM