ExcelDNA自定义Ribbon添加点击显示列表控件问题求助
解决Excel自定义Ribbon中添加点击显示列表控件的问题
嘿,我看你现在的代码只是给按钮加了个自定义工具提示,但完全没涉及到你要的「点击显示项目列表」的控件——这就是为啥没用啦!Excel Ribbon里有好几种控件能实现这个需求,我给你列几个最常用的方案,附带可直接用的代码示例:
1. 下拉菜单(DropDown):适合简单选项列表
这是最基础的列表控件,点击后展开选项列表,选择后触发回调。
自定义Ribbon XML代码:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"> <ribbon> <tabs> <tab id="myCustomTab" label="我的自定义标签"> <group id='myVersion2' label='About' visible='true'> <!-- 下拉菜单控件 --> <dropDown id="myDropDown" label="选择项目" getItemCount="GetDropDownItemCount" getItemLabel="GetDropDownItemLabel" onAction="DropDownSelected" screentip="选择一个项目" supertip="点击展开列表,选择你需要的选项"> </dropDown> </group> </tab> </tabs> </ribbon> </customUI>
对应的VBA回调函数(要放到Excel的模块里):
' 返回下拉菜单的选项数量 Sub GetDropDownItemCount(control As IRibbonControl, ByRef count) count = 3 ' 假设你有3个选项 End Sub ' 返回每个选项的显示文本 Sub GetDropDownItemLabel(control As IRibbonControl, index As Integer, ByRef label) Select Case index Case 0: label = "项目1" Case 1: label = "项目2" Case 2: label = "项目3" End Select End Sub ' 选择选项后的触发事件 Sub DropDownSelected(control As IRibbonControl, id As String, index As Integer) MsgBox "你选择了:" & Choose(index + 1, "项目1", "项目2", "项目3") End Sub
2. 图库控件(Gallery):适合带图标的可视化列表
如果你的选项需要配图标,用Gallery更直观,它会展示带图标的选项网格或列表。
自定义Ribbon XML代码:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"> <ribbon> <tabs> <tab id="myCustomTab" label="我的自定义标签"> <group id='myVersion2' label='About' visible='true'> <!-- 图库控件 --> <gallery id="myGallery" label="选择功能" getItemCount="GetGalleryItemCount" getItemImage="GetGalleryItemImage" getItemLabel="GetGalleryItemLabel" onAction="GallerySelected" columns="3" rows="1" screentip="选择一个功能" supertip="点击展开图标列表,选择对应的功能"> </gallery> </group> </tab> </tabs> </ribbon> </customUI>
对应的VBA回调函数:
' 返回图库的选项数量 Sub GetGalleryItemCount(control As IRibbonControl, ByRef count) count = 3 End Sub ' 返回每个选项的图标(这里用Excel内置图标,你也可以用自定义图片路径) Sub GetGalleryItemImage(control As IRibbonControl, index As Integer, ByRef image) Select Case index Case 0: Set image = Application.CommandBars.GetImageMso("FileSave", 16, 16) Case 1: Set image = Application.CommandBars.GetImageMso("FileOpen", 16, 16) Case 2: Set image = Application.CommandBars.GetImageMso("FilePrint", 16, 16) End Select End Sub ' 返回每个选项的文本标签 Sub GetGalleryItemLabel(control As IRibbonControl, index As Integer, ByRef label) Select Case index Case 0: label = "保存文件" Case 1: label = "打开文件" Case 2: label = "打印文件" End Select End Sub ' 选择图库选项后的触发事件 Sub GallerySelected(control As IRibbonControl, id As String, index As Integer) Select Case index Case 0: ActiveWorkbook.Save Case 1: Application.GetOpenFilename Case 2: ActiveWorkbook.PrintOut End Select End Sub
3. 拆分按钮(SplitButton):主按钮+下拉列表
如果你需要一个默认操作按钮,同时点击右侧箭头展开更多选项,SplitButton是最佳选择。
自定义Ribbon XML代码:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"> <ribbon> <tabs> <tab id="myCustomTab" label="我的自定义标签"> <group id='myVersion2' label='About' visible='true'> <!-- 拆分按钮控件 --> <splitButton id="mySplitButton" screentip="快速操作"> <!-- 主按钮 --> <button id="mainBtn" label="默认操作" onAction="MainBtnClick" imageMso="FileSave" /> <!-- 下拉菜单 --> <menu id="splitMenu" itemSize="large"> <button id="menuItem1" label="选项1" onAction="MenuItem1Click" imageMso="FileOpen" /> <button id="menuItem2" label="选项2" onAction="MenuItem2Click" imageMso="FilePrint" /> <button id="menuItem3" label="选项3" onAction="MenuItem3Click" imageMso="FileNew" /> </menu> </splitButton> </group> </tab> </tabs> </ribbon> </customUI>
对应的VBA回调函数:
' 主按钮点击事件 Sub MainBtnClick(control As IRibbonControl) MsgBox "你点击了默认操作按钮!" End Sub ' 下拉选项1点击事件 Sub MenuItem1Click(control As IRibbonControl) MsgBox "你选择了选项1" End Sub ' 下拉选项2点击事件 Sub MenuItem2Click(control As IRibbonControl) MsgBox "你选择了选项2" End Sub ' 下拉选项3点击事件 Sub MenuItem3Click(control As IRibbonControl) MsgBox "你选择了选项3" End Sub
注意事项:
- 你需要用「Custom UI Editor for Office」这类工具把XML代码导入到Excel文件里,或者用Office开发工具里的「RibbonX」相关功能。
- 所有回调函数的名称必须和XML里的
getItemCount、onAction等属性值完全一致,大小写也要匹配。 - 如果用自定义图标,要把图片嵌入到Excel文件里,然后在XML里引用图片的ID。
内容的提问来源于stack exchange,提问作者Dany M
相关产品推荐
相关产品推荐

