The history of posts on the Ribbon can be found at the Ribbon Example Table of Contents.
In this article I'm going to demonstrate a very simple menu created on the Ribbon.Â This particular menu will only hold two buttons on it, but we'll get into some larger examples in future posts.
The XML Markup provided below is very slim.Â It still adds our XLG tab and a Testing group, but only adds one menu item on that tab.Â The rest of the menu items will be added dynamically at runtime.
VBA Code Required
Unlike the last couple of examples, no code is required in the ThisWorkbook module at this point.Â We will need to add our callback code to populate the menu, and also the onAction code to react when a button is clicked.Â That code is all shown below:
[vb]Private Sub GetContent(control As IRibbonControl, ByRef returnedVal)
'Populate a menu item
Dim sXML As String
'Open the XML string
sXML = "
'Return the completed XML to the RibbonUI
returnedVal = sXML
Private Sub btnCentral(control As IRibbonControl)
'Feed back which button was clicked
MsgBox "You clicked " & control.ID
The code above will result in a menu that looks like this:
How it works
Clicking "My Menu" will result in the GetContent routine being fired.Â This routine is looking to pass the XML back to the RibbonUI so that it can be "merged" into the RibbonX code and populate the child items.Â The code requires no line formatting, so we basically just build a string of XML to create a menu.Â The code in the GetContent routine must start with the customui line, and be properly formed XML.
To demonstrate this a little clearer, the dynamic XML we created above to populate the buttons, (with formatting added,) is shown below:
And, of course, because we specified the OnAction parameter as "btnCentral", it will fire the btnCentral routine when we click either button.Â That code is set to tell us what button we clicked.