I'm making a Custom Tab for Excel with Custom UI Editor and I have two DropDown controls in it. Let's call them DropDown1 and DropDown2. My goal is that whenever I change the DropDown1 selection it automatically changes de DropDown2 selection, but I don't know how to set the "SelectedItem" in a DropDown Control.
So far I have a VBA function which is triggered every time I change the selection of DropDown1, I think that can be helpfull.
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
来源:https://stackoverflow.com/questions/43575080/how-to-set-selected-item-on-custom-dropdown-ribbon-control