|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
潜水学习知识多年,分享一下此知识点,VBE菜单我是向罗刚君的程序开发自学宝典第2版学习过来的,书上讲的是VBA代码,上周末我研究出来了在VSTO中实现,VB.net和VBA实现菜单单击不一样,“AddHandler 按钮.Click, AddressOf 过程名”此句代码是实现菜单单击的关键。
1、首先在ThisAddIn的Startup添加一行代码,目的是加载插件的时间呼叫创建菜单过程
Private Sub ThisAddIn_Startup() Handles Me.Startup
Call 创建菜单()
End Sub
2、建立一个模块用于存放菜单的相关代码,例如:
Module VBE_CommandBar
Sub 创建菜单()
Dim obj As Object
Dim mycontrol2 As Microsoft.Office.Core.CommandBarPopup
Dim One_But1 As Microsoft.Office.Core.CommandBarButton,One_But2 As Microsoft.Office.Core.CommandBarButton
On Error Resume Next
obj = app.VBE.ActiveVBProject
If Err.Number <> 0 Then MsgBox("请勾选""信任对VBA工程对象模型的访问!""") : Exit Sub
app.VBE.CommandBars("菜单条").Controls("VBA代码辅助大全").Delete()
With app.VBE.CommandBars("菜单条").Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, 1, , 3, 1) 'VBE菜单中第3个位置添加菜单
.Caption = "VBA代码辅助大全"
'_____________________________________________________________________
mycontrol2 = .Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, 1, , , True) '添加单句代码二级子菜单
With mycontrol2
.Caption = "单句代码"
One_But1= .Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, 1,,, True)
One_But1.Caption = "添加行号"
One_But1.Style =Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
One_But1.FaceId = 79
AddHandler One_But1.Click, AddressOf 为代码添加行号
'_____________________________________________________________________
One_But2= .Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, 1,,, True)
One_But2.Caption = "添加注释"
One_But2.Style =Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
One_But2.FaceId = 80
AddHandler One_But2.Click, AddressOf 添加注释
End With
End With
End Sub
Sub 为代码添加行号()
MsgBox("你单击了按钮1")
End Sub
Sub 添加注释()
MsgBox("你单击了按钮2")
End Sub
End Module
3、VS软件选择启动,就可以看到自己建的两个菜单了。
|
|