|
楼主 |
发表于 2016-5-31 21:58
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
SuperVBE.cls
- Option Explicit
- '本模块用于增强VBE的功能,主要表现在对菜单命令和工具栏命令的单击事件的拦截与增强,另外对引用的添加与移除事件作监听
- '主要应用举例:
- '比如不满意代码的加注释与取消注释功能,则可以通过拦截原事件的处理,并建立新的执行逻辑,比如所加的注释紧贴代码,而不是在行首
- '比如想新增一个按名称排列代码块的功能,就可以通过新增按钮及事件处理程序的方式来实现新功能
- '本模块异常列表【从-2147220850起,预留100个,最大到-2147220751】
- '-2147220850, "SuperVBE.AddCommandBarButton", "新创建的CommandBarButton的Name参数必须是一个合法的标识符"
- Public WithEvents CommandBarEvent As VBIDE.CommandBarEvents
- Public WithEvents References As VBIDE.ReferencesEvents
- Private Sub CommandBarEvent_Click(ByVal CommandBarControl As Object, Handled As Boolean, CancelDefault As Boolean)
- If CommandBarControl.BuiltIn = False Then
- Select Case TypeName(CommandBarControl)
- Case "CommandBarButton"
- Dim cbbtn As CommandBarButton
- Set cbbtn = CommandBarControl
- Application.Run cbbtn.OnAction
- Case "CommandBarComboBox"
- Dim cbcbb As CommandBarComboBox
- Set cbcbb = CommandBarControl
- Application.Run cbcbb.OnAction
- Case "CommandBarPopup"
- Dim cbp As CommandBarPopup
- Set cbp = CommandBarControl
- Application.Run cbp.OnAction
- Case Else
- '无法识别的类型
- End Select
- End If
- End Sub
- Private Sub References_ItemAdded(ByVal Reference As VBIDE.Reference)
- '监听并增强本VBA工程引用的添加
-
- End Sub
- Private Sub References_ItemRemoved(ByVal Reference As VBIDE.Reference)
- '监听并增强本VBA工程中对引用的移除
-
- End Sub
复制代码
下面是一个试验代码:
- Public xVBE As New SuperVBE
- Public Sub AAAA()
- On Error Resume Next
- Application.VBE.CommandBars("标准").Controls("试验").DELETE
- Dim cbb As CommandBarButton
- Set cbb = Application.VBE.CommandBars("标准").Controls.Add(msoControlButton)
- cbb.Caption = "试验"
- cbb.style = msoButtonCaption
- cbb.OnAction = "AAAAA"
- Set xVBE.CommandBarEvent = Application.VBE.Events.CommandBarEvents(Application.VBE.CommandBars("标准").Controls("试验"))
- End Sub
- Public Sub AAAAA()
- MsgBox "I'm Clicked!"
- End Sub
复制代码
执行一下AAAA过程,就可看到效果 |
|