您需要在自定义 UI 编辑器中向功能区 XML 添加回调函数,然后将相应的代码添加到 VBA 项目中,以便在功能区选项卡失效时调用。为下拉控件设置所选项目所需的回调是getSelectedItemIndex
or getSelectedItemID
,取决于您是想按索引还是按 id 选择项目。由于您没有提供任何代码,因此我的示例是通用的(未经测试):
功能区 XML:
<dropDown id="drpTest" label="Test dropdown" getSelectedItemIndex="drpTestGetSelectedItem" ></dropDown>
VBA回调
'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:
基于其他下拉列表选择索引的示例。在类似的解决方案中,我在中设置了一个值onAction
一个控件的功能,并用它来设置另一个控件中选定的索引,如下所示:
功能区 XML:
<dropDown id="drpTest1" label="Test dropdown 1" onAction="drpTest1OnAction" ></dropDown>
<dropDown id="drpTest2" label="Test dropdown 2" getSelectedItemIndex="drpTest2GetSelectedItem" ></dropDown>
VBA 回调
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