Question

I have created a custom ribbon in Microsoft Word but I am having issues attaching events to the buttons found in a ribbon. Below is my code:

UI XML:

<mso:cmd app="Word" dt="1" />
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon startFromScratch="true" >
    <tabs>
      <tab id="CustomTab" label="MyTasks" >
           <group id="Group1" label="Details Labels">
             <menu id="Menu1" label="Details" size="large">
                <menu id="Menu21" label="Dates">
                   <button id="my_date" onAction="foo_eventhandler" label="Some Date" />
                </menu>
             </menu>
         </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

I then have the following VBA code in Modules/NewMacros VBA code:

Sub foo_eventhandler(control As IRibbonControl)

End Sub

NOTE:

I import the XML by opening Word-->going to File-->Options-->Customize Ribbon-->Import/Export. I then select my XML file and import it.

When this code is run, though, I get the error "Argument Not Optional". If I run the same code without the "control As IRibbonControl" it's fine but I need to be able to get the Sender object. Anyone have any suggestions?

jason

Was it helpful?

Solution

I think you are running into problems because you are importing the XML code via Options -> Customize Ribbon ->Import/Export. This method is really for general users who wish, for example, to never see the Page Layout tab and have it hidden. They can export their custom ribbon and import it onto new machines for the same layout.

For developers a better method is use the most excellent CustomUIEditor for Word and Excel. http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/08/07/7293.aspx

The steps you need to take are to create your normal macro enabled Word template (the .dotm) file and save it. Then open that file up in the CustomUIEditor and paste you XML (minus the first line). I've expanded your XML code with another button and I've added the tag to the XML so the VBA knows which button is being pressed.

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon startFromScratch="false" >
    <tabs>
      <tab id="CustomTab" label="MyTasks" >
           <group id="Group1" label="Details Labels">
             <menu id="Menu1" label="Details" size="large">
                <menu id="Menu21" label="Dates">
                   <button id="my_date_1" onAction="foo_eventhandler" label="Some Date" tag="Date1" />
                   <button id="my_date_2" onAction="foo_eventhandler" label="Some Other Date" tag="Date2" />
                </menu>
             </menu>
         </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

I generally put my Ribbon code in a Ribbon module. In your callback code you just need to use the .Tag property of the inputted control variable to know which button is pressed which you'll see corresponds to the tag in the XML. ie:

Sub foo_eventhandler(control As IRibbonControl)
    MsgBox "Hooray! for " & control.Tag
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top