MS-Excel / General Formatting

Resetting Controls

The attribute callback macros run when RibbonX initializes your custom interface. What happens, however, if a change of state in your program also requires a change of state in a custom Ribbon control?

To do this, you have to let VBA know that your Ribbon interface exists. This requires three things. First, you need to add a module-level variable declared with the IRibbonUI type:

Private myRibbon As IRibbonUI

Second, you include the following procedure to initialize the myRibbon object:

Sub Ribbon_OnLoad(ByVal ribbon As Office.IRibbonUI)
    Set myRibbon = ribbon)
End Sub

Finally, you add the onLoad attribute to the <customUI> element and set it equal to the name of your initialization procedure:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="Ribbon customization_OnLoad">

The significance of these three steps is that your code can now work with an IRibbonUI object, which has no properties but does come with two important methods:

ribbon.Invalidate()
ribbon.InvalidateControl(ControlID)
ribbon
An IRibbonUI object.
ControlID
A string value that specifies the ID of a Ribbon control in your custom XML.

You use these methods to reinitialize some or all of your custom interface. If you run InvalidateControl, RibbonX reinitializes the specified control; if you run Invalidate, instead, RibbonX reinitializes every one of your custom controls.

If you have many controls, or if you have controls with lengthy initialization callbacks, running the Invalidate method could take a while.Therefore, it's almost always best to use InvalidateControl, instead, so that you reinitialize only a specific control.

To see how this works, let's add a button to the same group as the combo box from the previous section:

<button id="btnRefreshList"
	label="Refresh List"
	imageMso="Refresh"
	onAction="Module1.btnRefreshList_OnAction" />

Here's the btnRefreshList_OnAction macro:

Sub btnRefreshList_OnAction(ByVal control As IRibbonControl)
    myRibbon.InvalidateControl "combobox1"
End Sub

This procedure invalidates the btnRefreshList control. This tells RibbonX to run through all of that control's callback macros, which in this case serves to repopulate the list with the current set of open windows, as well as reset the edit box text.

[Previous] [Contents] [Next]