How to set selected item on Custom DropDown Ribbon Control

做~自己de王妃 提交于 2019-12-02 09:58:52

You need to add a callback function to you ribbon XML in the Custom UI Editor and then add the corresponding code to you VBA project to be called when the ribbon tab gets invalidated. The callback you need to set the selected item for the dropdown control is either getSelectedItemIndex or getSelectedItemID, depending on if you want to select the item by index or by id. Since you have not provided any code, my examle is general (and not tested):

Ribbon XML:

<dropDown id="drpTest" label="Test dropdown" getSelectedItemIndex="drpTestGetSelectedItem" ></dropDown>

VBA callback

'Callback for drpTest getSelectedItemIndex
Sub drpTestGetSelectedItem(control As IRibbonControl, ByRef returnedVal)
    returnedVal = 1   '***** To select the item with index 1,
                      '***** replace with code to select the desired item
End Sub

EDIT:

Example where index is selected based on other droplist. In similar solutions I have set a value in the onAction function of one control and used it to set the selected index in another control, something like the following:

Ribbon XML:

<dropDown id="drpTest1" label="Test dropdown 1" onAction="drpTest1OnAction" ></dropDown>
<dropDown id="drpTest2" label="Test dropdown 2" getSelectedItemIndex="drpTest2GetSelectedItem" ></dropDown>

VBA callbacks

Global myRibbon As IRibbonUI
Global giIndex As Integer

'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
    '***** Save reference to ribbon object to invalidate
    Set myRibbon = ribbon
End Sub

'Callback for drpTest1 onAction
Sub drpTest1OnAction(control As IRibbonControl, id As String, index As Integer)
    '***** Set selected item variable for drpTest2
    giIndex = index
    '***** Tell Excel to redraw ribbon
    '(you could invalidate only parts of the ribbon with InvalidateControl
    'or InvalidateControlMso)
    myRibbon.Invalidate
End Sub

'Callback for drpTest2 getSelectedItemIndex
Sub drpTest2GetSelectedItem(control As IRibbonControl, ByRef returnedVal)
    '***** Return selected item for drpTest2 based on value stored in giIndex
    returnedVal = giIndex
End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!