Search code examples
excelvbacomboboxribbonxcustom-ui

RibbonX - using comboBox to run macro / VBA


I am new to custom menus but have been successful at using Ribbon X to set up some custom tabs / groups in Excel. They are ribbon buttons that run macros (fairly basic stuff like hiding columns).

What I really want to do is to have a ribbon comboBox (or dropdown) which can be used to filter a table on specific columns. I have some macros which work - a simple example is filtering on a column called PRIORITY. I have a macro called "P1" which filters the table to show Priority 1 items "P2" for Priority 2 items etc. If I run these macros manually they have the desired effect.

Next step is how to get them to run from the menu.

I have tried this:

<group id="customGroup3" label="Content Filters">
    <comboBox id="comboBox1" label="Priority Filter">
        <item id="item2" label="P1" onAction="P1"/>
        <item id="item3" label="P2" onAction="P2"/>
    </comboBox>
</group>

This doesn't work because I get a validation error "The 'onAction' attribute is not declared.". I think I have read that you can't have "onAction" as an attrbute of a comboBox item.

What do I need to do to get macro P1 to run if P1 is selected in the comboBox and macro P2 if P2 is selected?


Solution

  • There is no onAction attribute for the item element in the Ribbon UI. Possible attributes are id, image, imageMso, label, screentip, supertip.

    It seems you are interested in the onChange callback of the combobox control which has the following signature:

    C#: void OnChange(IRibbonControl control, string text)
    
    VBA: Sub OnChange(control As IRibbonControl, text As String)
    
    C++: HRESULT OnChange([in] IRibbonControl *pControl, [in] BSTR *pbstrText)
    
    Visual Basic: Sub OnChange(control As IRibbonControl, text As String)
    

    Read more about possible controls and their attributes in the following articles: