2
votes

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. 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? I've not found such examples online:

Ribbon in VSTO example:

<?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. 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. And new office Add-in functionality does not support some functionalities that my legacy VSTO add-in does.

1
Adding a Ribbon to a document is one thing, attaching code is something else entirely. Where do you expect the code to reside? What, more exactly are you trying to achieve? Have you read this discussion stackoverflow.com/questions/4499425/…Cindy Meister
@CindyMeister. Good question. To answer your question, I've added an UPDATE section to my post above.nam

1 Answers

2
votes

What you imagine is unfortunately not possible.

By design, Ribbon XML is linked to the code in the same "container". Ribbon XML stored in an Office document must work with call-backs in the VBA code of the same document. 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. (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.)

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. The Ribbon's call-backs cannot be in any other "container", although the call-back code can call code outside the "container". (Note that a VSTO ribbon cannot be called by VBA code... and the Ribbon is not stored in any document.)

With Ribbon XML in VBA and COM add-ins it's possible to share Ribbon controls by assigning them a Q id. For shared controls, code in any container can refer to them, as long as the id is known.

The Ribbons defined in Office JS Add-ins are also dependent on the code being inside the same container. The Office JS model does not (yet?), however, work with the concept of shared controls.

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). The functionality is being extended steadily, recently with emphasis on 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. If the request has already been made, up-vote it. If you can't find it, create one.