Office中国论坛/Access中国论坛

标题: 求助:VBE右键菜单 [打印本页]

作者: 阿cc-ess-VBA    时间: 2016-11-26 07:18
标题: 求助:VBE右键菜单
请问如何为新添加的右键菜单添加按钮事件
Sub add()
    With Application.VBE.CommandBars("Code Window").Controls.add
        .Caption = "右键测试"
        .FaceId = 1025
        如何添加按钮事件
    End With
End Sub

作者: tmtony    时间: 2016-11-26 08:31
需要 使用withevent 才能 使用事件
作者: 阿cc-ess-VBA    时间: 2016-11-26 08:34
tmtony 发表于 2016-11-26 08:31
需要 使用withevent 才能 使用事件

老师可否详细讲下,谢谢!
作者: tmtony    时间: 2016-11-26 10:10
Option Explicit
‘ 定义CommandButton的对象变量cmd,使用WithEvents关键字,用来引用CommandButton对象的事件,cmd变量作为MyClass的属性
Public WithEvents cmd As CommandButton

要 set cmd=Application.VBE.CommandBars("Code Window").Controls.add .........


‘ 为cmd对象编写Click事件过程(这里就是编写的通用事件)
Private Sub cmd_Click()
  MsgBox cmd.Caption  ‘弹出对话框,显示cmd对象的Caption名
End Sub
作者: 阿cc-ess-VBA    时间: 2016-11-26 14:04
tmtony 发表于 2016-11-26 10:10
Option Explicit
‘ 定义CommandButton的对象变量cmd,使用WithEvents关键字,用来引用CommandButton对象 ...

谢谢老师!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3