I am trying to get a toggle button in the Word 2019 ribbon.
I have viewed many examples here on Stack Overflow, but I did not get it running.
My XML looks like:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="loadRibbon">
<ribbon>
<tabs>
<tab id="doc_management" label="Publishing" insertBeforeQ="TabDeveloper">
<group id="doc_drafting" label="Drafting" autoScale="true">
<toggleButton id="toggling" label="Insert Watermark" imageMso="WatermarkGallery" onAction="togglingWatermark" getPressed="buttonPressed"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
The corresponding VBA code:
Option Explicit
Public myRibbon As IRibbonUI
Public isPressed As Boolean
Sub loadRibbon(ribbon As IRibbonUI)
Set myRibbon = ribbon
End Sub
Sub togglingWatermark(control As IRibbonControl, pressed As Boolean)
Select Case control.ID
isPressed = pressed
Case "toggling"
If isPressed Then
MsgBox isPressed
Else
MsgBox isPressed
End If
End Select
myRibbon.InvalidateControl control.ID
End Sub
Sub buttonPressed(control As IRibbonControl, ByRef returnedVal)
Select Case control.ID
Case "toggling"
returnedVal = isPressed
End Select
End Sub
The custom tab with corresponding button is present. I can also toggle the button. But I would expect to see a popup screen as soon as I toggle. And that is not happening. What am I missing? Any help would be very much appreciated!
EDIT:
It seems an instance of the IRibbonUI is not loaded. So it doesn't allow me to invalidate or use the callbacks in the first place. I verified this by adding MsgBox("Loaded")
in the loadRibbon
Sub. In my understanding, a message box should appear as soon as a Word document is opened.
The VBA code is saved in a macro-enabled Word template and stored in %appData%\Microsoft\Word\STARTUP. Adding a normal button that calls a Sub from the same template is working.
I finally found the problem and have got it all up and running now. As other people may run into the same issue, I explain what I did.
One simple way of customising the ribbon with an extension written by someone else is to go to File > Options > Customize Ribbon and then choose "Import customization file" from the "Import/Export" dropdown box in the bottom right corner. This will create a file in %localAppData%\Microsoft\Office called "Word.officeUI".
I thought it to be a good idea to write my own xml code directly into this file. It works... as long as you don't want to dynamically update the ribbon. As the initial version of the ribbon didn't require dynamic updates, it was absolutely fine. For that reason, I hadn't this Office RibbonX Editor installed either. As soon as I implemented my customisation with this tool the problem was solved (and many other created, but all solvable).