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.