Excel VSTO开发11-自定义菜单项

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

11 自定义菜单项

自定义菜单项可以在插件启动时候添加,即增加到ThisAddIn_Startup() 内。

下面以具体代码说明:

代码7\] 增加自定义菜单项 '插件启动时候 Private Sub ThisAddIn_Startup() Handles Me.Startup ...... Call addMenu() End Sub Private Sub addMenu() '单元格右键时候的弹出菜单 Dim cMenuControl As Office.CommandBar = Globals.ThisAddIn.Application.CommandBars("Cell") '增加菜单项到菜单第一个 Dim cButtonControl As Microsoft.Office.Core.CommandBarControl = cMenuControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, Before:=1) '转换为CommandBarButton Dim cButton As Microsoft.Office.Core.CommandBarButton = CType(cButtonControl, Microsoft.Office.Core.CommandBarButton) cButton.Caption = "查看菜单" cButton.Tag = "这是自定义菜单项" cButton.FaceId = 22 '增加菜单项按下事件 AddHandler cButton.Click, AddressOf menu_click End Sub 以上代码运行后会在单元格右键菜单上增加一个"查看菜单"菜单项,位置在第一个。如下图所示: ![](https://file.jishuzhan.net/article/1700852870951211010/de4147a761d34d93b7ecaa164060ac2d.jpeg) ******图**** ****23**** ****增加自定义菜单项****** 如果把代码中 Before:=1 修改为 Before:=5 那么菜单项将增加到第5个位置,如下图所示: ![](https://file.jishuzhan.net/article/1700852870951211010/ae4d36eda88245fdbf202ada21f29ea3.jpeg) ******图**** ****2**** ****4**** ****自定义菜单项位置改变后****** 在实际中,以上代码会存在一定问题,读者可以测试,当运行多次后会发现多出很多相同的菜单项,也就是菜单项重复被添加。 修改后的代码如下: Private Sub addMenu() '单元格右键时候的弹出菜单 Dim cMenuControl As Office.CommandBar = Globals.ThisAddIn.Application.CommandBars("Cell") '列出当前菜单的所有菜单项 For Each cControl As Microsoft.Office.Core.CommandBarControl In cMenuControl.Controls '下面一句代码可以输出当前所有菜单项 'System.Diagnostics.Debug.WriteLine(cControl.Caption \& " " \& cControl.Id) ' 如果已经存在我们自定义的菜单项 If cControl.Caption = "查看菜单" Then '1、可以退出 'Exit Sub '2、可以将之前已经存在的菜单项删除 cControl.Delete() End If Next '增加菜单项到菜单第一个 Dim cButtonControl As Microsoft.Office.Core.CommandBarControl = cMenuControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, Before:=1) '转换为CommandBarButton Dim cButton As Microsoft.Office.Core.CommandBarButton = CType(cButtonControl, Microsoft.Office.Core.CommandBarButton) cButton.Caption = "查看菜单" cButton.Tag = "这是自定义菜单项" cButton.FaceId = 22 '增加菜单项按下事件 AddHandler cButton.Click, AddressOf menu_click End Sub 顺便将自定义菜单项的事件增加如下: '菜单项按下事件:列出所有的命令栏ID和名称 Public Sub menu_click(ByVal control As Office.CommandBarButton, ByRef cancelDefault As Boolean) '在Excel里面随时记住从1开始,而非vb中的0开始 Dim i As Integer = 1 Dim Wbook As Excel.Workbook = Application.Workbooks(1) Dim Wsheet As Excel.Worksheet Wsheet = Wbook.Worksheets("sheet1") For Each cMenuControl As Office.CommandBar In Globals.ThisAddIn.Application.CommandBars Wsheet.Cells(i, 1).Value = cMenuControl.Id Wsheet.Cells(i, 2).Value = cMenuControl.Name Select Case cMenuControl.Type Case 0 Wsheet.Cells(i, 3).Value = "msoBarTypeNormal" Case 1 Wsheet.Cells(i, 3).Value = "msoBarTypeMenuBar" Case 2 Wsheet.Cells(i, 3).Value = "msoBarTypePopup" Case Else End Select i += 1 Next MsgBox("ok") End Sub

相关推荐
林月明2 小时前
【VBA】自动设置excel目标列的左邻列格式
开发语言·excel·vba·格式
JavaOpsPro5 小时前
审计 jenkins获取构建历史,生成excel
运维·jenkins·excel
CodeCraft Studio7 小时前
国产化Excel开发组件Spire.XLS教程:在Python中将Pandas DataFrame导出到Excel的详细教程
python·excel·pandas
电话交换机IPPBX-3CX7 小时前
在 MS Excel 和 Google Sheets 中生成 3CX 可视化通话报告
excel·ip pbx·电话交换机·google表格·可视化报表
知识分享小能手9 小时前
uni-app 入门学习教程,从入门到精通,uni-app中uCharts组件学习((8)
vue.js·学习·ui·微信小程序·小程序·uni-app·echarts
星空的资源小屋13 小时前
Antares SQL,一款跨平台开源 SQL 客户端
数据库·人工智能·pdf·开源·电脑·excel·1024程序员节
Aevget14 小时前
界面控件Kendo UI for Angular 2025 Q3亮点 - 全新的AI编码助手
ui·界面控件·kendo ui·ui开发·1024程序员节
萌新小码农‍1 天前
SpringBoot+alibaba的easyexcel实现前端使用excel表格批量插入
前端·spring boot·excel
油炸自行车1 天前
【Qt】VS Code配置Qt UI插件,vscode打开Qt Designer ,vscode打开Qt*.ui文件
qt·ui·cursor·qt designer·vscode插件·trae·qt ui
程序员小远1 天前
Appium+python+unittest搭建UI自动化框架
自动化测试·软件测试·python·测试工具·ui·appium·测试用例