让下拉菜单在自定义 Excel 功能区中工作

Getting a Dropdown to Work in a Customised Excel Ribbon

所以我正在使用 Office RibbonX 编辑器和 Excel 创建自定义功能区。我想显示一个简单的下拉菜单,允许用户在三个工作表之间切换。

我不确定为什么会出错,但我将 运行 分为三个错误,对应于每个项目的 onAction 属性:

Ln 8, Col 51: The 'onAction' attribute is not declared.
Ln 9, Col 51: The 'onAction' attribute is not declared.
Ln 10, Col 51: The 'onAction' attribute is not declared.

这是我的代码:

XML(customUI14.xml 文件):

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
        <tabs>
            <tab id="customTab" label="DropDown" insertAfterMso="TabHome">
                <group id="customGroup" label="Custom Group">
                    
<dropDown id="dropDown1" label="Dropdown Box">
   <item id="dd01" label="Sheet1" imageMso = "_1" onAction="DDOnAction" />
   <item id="dd02" label="Sheet2" imageMso = "_2" onAction="DDOnAction" />
   <item id="dd03" label="Sheet3" imageMso = "_3" onAction="DDOnAction" />
</dropDown>

                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

VBA:

Sub DDOnAction(control As IRibbonControl, id As String, index As Integer)
    Select Case control.id
        Case "dd01"
            ActiveWorkbook.Sheets("Sheet1").Activate
        Case "dd02"
            ActiveWorkbook.Sheets("Sheet2").Activate
        Case "dd03"
            ActiveWorkbook.Sheets("Sheet3").Activate
    End Select
    
    End Sub

您好,onAction 属性应该放在下拉列表中而不是项目中, 所以 XML 是:

    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
        <tabs>
            <tab id="customTab" label="DropDown" insertAfterMso="TabHome">
                <group id="customGroup" label="Custom Group">
                    
<dropDown id="dropDown1" label="Dropdown Box" onAction="DDOnAction"  >
   <item id="dd01" label="Sheet1" imageMso = "_1" />
   <item id="dd02" label="Sheet2" imageMso = "_2" />
   <item id="dd03" label="Sheet3" imageMso = "_3" />
</dropDown>

                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

代码是这样的

Sub DDOnAction(control As IRibbonControl, id As String, Index As Integer)
Select Case Index
    Case 0
        ActiveWorkbook.Sheets("Sheet1").Activate
    Case 1
        ActiveWorkbook.Sheets("Sheet2").Activate
    Case 2
        ActiveWorkbook.Sheets("Sheet3").Activate
End Select

End Sub