您需要使用功能区对象的InvalidateControl method 来使缓存无效并更新组合框。
首先,如果您还没有这样做,您需要在 RibbonX 代码中为您的 customUI 标签包含一个 onLoad 参数。此参数将指定打开工作簿时要执行的 VBA 回调过程,以便可以创建功能区对象。
然后,您需要包含 getItemCount、getItemLabel 和 getItemID 的 VBA 回调过程。当组合框无效时,将调用 getItemCount。然后,依次调用 getItemLabel 和 getItemID。
getItemCount 过程用于指定要包含在组合框中的项目数。因此,getItemLabel 和 getItemID 都被调用了相同的次数,以便可以相应地更新 ID 和标签。
然后,当然,您需要包含将分配给您的按钮的过程,这实际上会使组合框无效。
由于您尚未提供所有相关详细信息,因此这里有一个示例,希望您能够根据自己的需要进行调整。
RibbonX 代码
<!--RibbonX Visual Designer 2.33 for Microsoft Excel CustomUI14 . XML Code produced on 2020/05/16-->
<customUI
xmlns="http://schemas.microsoft.com/office/2009/07/customui"
onLoad="Initialize">
<ribbon >
<tabs >
<tab
id="Tab1"
label="Tab1">
<group
id="Group1"
label="Group1">
<comboBox
id="Combo3"
label="MyCombobox"
getItemCount="Combo3_getItemCount"
getItemID="Combo3_getItemID"
getItemLabel="Combo3_getItemLabel"
getText="Combo3_getText"
onChange="Combo3_onChange"/>
</group >
</tab >
</tabs >
</ribbon >
</customUI >
VBA 代码
Option Explicit
Dim myRibbon As IRibbonUI
'Callback for customUI.onLoad
Sub Initialize(ribbon As IRibbonUI)
Set myRibbon = ribbon
End Sub
'Callback for Combo3 getItemCount (called once when the combobox is invalidated)
Sub Combo3_getItemCount(control As IRibbonControl, ByRef returnedVal)
returnedVal = 10 'the number of items for combobox
End Sub
'Callback for Combo3 getItemID (called 10 times when combobox is invalidated)
Public Sub Combo3_getItemID(control As IRibbonControl, index As Integer, ByRef id)
id = "ComboboxItem" & index + 1
End Sub
'Callback for Combo3 getItemLabel (called 10 times when combobox is invalidated)
Sub Combo3_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
returnedVal = "Item" & index + 1
End Sub
'Callback for Combo3 getText
Sub Combo3_getText(control As IRibbonControl, ByRef returnedVal)
returnedVal = "" 'clears the text from the combobox
End Sub
'Callback for Combo3 onChange
Sub Combo3_onChange(control As IRibbonControl, text As String)
MsgBox "You have chosen " & text
End Sub
Sub UpdateCombo3()
myRibbon.InvalidateControl "Combo3" 'invalidates the cache for the combobox
End Sub