RibbonX and VBA

As well as having the ability to create custom tabs and groups containing built-in controls, Microsoft has provided the ability to add many types of custom controls to the Ribbon, and to hook their actions and most of their attributes to VBA procedures and functions. This is done using a mechanism known as a callback. A callback means simply that, as part of a RibbonX definition, you provide the name of a procedure to run when the control is clicked, changed, and so on. It's exactly the same as you've been doing for years with Application.OnKey, CommandBarButton.OnAction, and so forth. The main difference in RibbonX is that it passes a number of parameters to the function being called, so the function signature must be declared correctly for the call to work. Again, that is no different than how you've been coding UserForm control event procedures (other than the function signature usually being written for you).

Callbacks are also used when you need to be able to change a control's attributes at run time. Rather than defining a specific value for the attribute in the XML, provide the name of a procedure that Excel should call whenever it needs to know the attribute's value; it's up to that procedure to work out (and remember) what the value should be and return it to Excel. For example, rather than including the label="Miscellaneous" attribute in your earlier custom group, you could have specified a getLabel callback:

<group id="rxAuditMisc" getLabel="rxAuditMisc_getLabel" >

When the tab is first displayed, Excel will call the rxAuditMisc_getLabel VBAprocedure (in a standard module), which should provide the text for Excel to use. If you want to change that text sometime later, you can't just update an object's property; RibbonX does not have an object model. Instead, there's an interface to tell Excel that the information provided in an earlier callback is no longer valid; when Excel next needs to display the group or control, it will call the procedure again to get the new value. Most attributes of most controls can be set dynamically in this way. All the available callbacks and their function signatures are detailed later in the chapter, but first, have a look at all the available types of custom controls you can add to the Ribbon and all the attributes you can set to modify their appearance and behavior.

+1 0

Responses

Post a comment