locked
Callbacks from ribbon in Word templates RRS feed

  • Question

  • Is there any way of getting a VSTO Word addin to respond directly to a callback from a Ribbon button embedded within a template in Word?

    As an example, I have the following ribbon code in a Word template

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"  >
     <ribbon>
      <tabs>
       <tab idMso="TabHome" >
        <group id="Documents" label="Documents" insertBeforeMso="GroupClipboard"
          tag="!!label=Documents">
          <button id="Button1" label="Label" onAction="RunButtonVSTO" />
        </group>
       </tab>
      </tabs>
     </ribbon>
    </customUI>

    The VBA callback would be in the form

    Sub RunButtonVSTO(control As IRibbonControl)
    End Sub

    But I want the callback to be to a VSTO ad-in using VB.NET, and not to be routed via any VBA.

    I know how to do this for both Designer ribbons and XML ribbons within the VSTO project. I specifically want to the VSTO project to respond to ribbon buttons within a Word template.

    Is this possible?

    Monday, May 2, 2016 11:21 PM

Answers

  • Hi, Jonathan West_

    Thanks very much for your details to help us figure out your issue. We can not find any solution has the ribbonXML in the template but the callbacks in the Add-in. But this workaround is that you could insert callbacks macro code into Word template in the Add-in.

    For more information, click here to refer about How To Create an Excel Macro by Using Automation from Visual C# .NET
    Thursday, May 5, 2016 5:56 AM

All replies

  • >>>But I want the callback to be to a VSTO ad-in using VB.NET, and not to be routed via any VBA.

    I know how to do this for both Designer ribbons and XML ribbons within the VSTO project. I specifically want to the VSTO project to respond to ribbon buttons within a Word template.<<<

    According to your description, you could add the following method to your Ribbon class. This is a callback method for the "Button1" button, refer to below:
    Public Sub RunButtonVSTO(ByVal control As Office.IRibbonControl)
    
         .......    
    
    End Sub

    For more information, click here to refer about Walkthrough: Creating a Custom Tab by Using Ribbon XML

    Tuesday, May 3, 2016 1:39 AM
  • I have the correct callback. But when I click the button I get the message "The macro can't be found or has been disabled because of your Macro security settings"

    That suggests to me that that Word can't see the callback to call it and so is looking for a callback in VBA and can't find it.

    When there is a separate ribbon associated with the project, the following routine gets included in the ribbon module

    Public Function GetCustomUI(ByVal ribbonID As String) As String Implements Office.IRibbonExtensibility.GetCustomUI
        Return GetResourceText("MyAddin.Ribbon1.xml")
    End Function

    I suspect that needs to be modified, but I don't know in what way.

    Tuesday, May 3, 2016 9:02 AM
  • >>>I have the correct callback. But when I click the button I get the message "The macro can't be found or has been disabled because of your Macro security settings"<<<

    According to your description, I have made a sample to try to reproduce this issue, unfortunately, I am not able. You could refer to below steps and code:

    1. Create "MineRibbon" XML Ribbon
    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso="TabHome" >
            <group id="Documents" label="Documents" insertBeforeMso="GroupClipboard"
              tag="!!label=Documents">
              <button id="Button1" label="Label" onAction="RunButtonVSTO" />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    2. Add code in MineRibbon.vb
    Public Sub RunButtonVSTO(ByVal control As Office.IRibbonControl)
            System.Windows.Forms.MessageBox.Show("Test")
    End Sub
    3. Add codes in ThisDocument.vb
    Protected Overrides Function CreateRibbonExtensibilityObject() As Microsoft.Office.Core.IRibbonExtensibility
            Return New MineRibbon()
    End Function

    Wednesday, May 4, 2016 3:04 AM
  • You have not accurately reproduced my original description.

    Create a word template and using the Custom UI Editor, embed the ribbon XML within the template. do not create the ribbon within the VSTO project.

    Create the callback as you have described.

    Open the template and click the button. The error message is displayed.

    Let me emphasise that I am looking to have the addin contain the callbacks which are referred to within ribbonXML embedded within a Word template.

    I know very well how to have VBA in a Word, Excel or PPT addin respond to callbacks in RibbonXML embedded within an Office file (e.g. a dotx template). I want to be be able to continue to have the ribbonXML in the template but the callbacks in the addin. Is this possible?

    Wednesday, May 4, 2016 8:31 AM
  • Let me explain a little more what I'm trying to achieve. I have a VBA addin at present, with a substantial code library. It is a .dotm file which is auto-loaded in Word by virtue of being in Word's STARTUP folder.

    I also have a number of different Word templates each with a different set of features and therefore a different range of custom ribbon buttons.The ribbon code I quoted has been embedded within a template using the technique described here http://gregmaxey.mvps.org/word_tip_pages/customize_ribbon_main.html

    Each of my templates has its own set of ribbon buttons embedded. The VBA code in the .dotm addin contains all the callbacks from all of the templates.

    I want to leave the ribbon buttons where they are and move the VBA code into a VB.NET project so that the callbacks are now made to there instead of the the VBA code in the .dotm file.

    Wednesday, May 4, 2016 8:51 AM
  • Hi, Jonathan West_

    Thanks very much for your details to help us figure out your issue. We can not find any solution has the ribbonXML in the template but the callbacks in the Add-in. But this workaround is that you could insert callbacks macro code into Word template in the Add-in.

    For more information, click here to refer about How To Create an Excel Macro by Using Automation from Visual C# .NET
    Thursday, May 5, 2016 5:56 AM
  • Thanks. Not the answer I wanted to hear, but better to know that than continue scouring the internet for a solution that doesn't exist.

    I can think of a few possible workarounds though none would be as elegant as the solution I was looking for.

    Thursday, May 5, 2016 9:13 AM