获取单击的菜单项的名称

时间:2015-05-08 06:04:49

标签: excel-vba drop-down-menu vba excel

有没有办法获取点击菜单项的名称? 我创建了一个带菜单和子菜单的新加载项文件。我可以获取单击要在宏内使用的子菜单的名称吗?

例如:

Sub Auto_Open()

Dim NewControl As CommandBarPopup
Dim mItem, SubMenu
On Error Resume Next
Call Auto_Close

Set NewControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add(Type:=msoControlPopup, temporary:=True)
NewControl.Caption = "&Menu"

Set mItem = NewControl.Controls.Add(Type:=msoControlButton)
With mItem
    .Caption = "Sub-Menu1"
    .OnAction = "submenu1"
End With

Set mItem = NewControl.Controls.Add(Type:=msoControlButton)
With mItem
    .Caption = "Sub-Menu2"
    .OnAction = "submenu2"
End With

Set mItem = NewControl.Controls.Add(Type:=msoControlButton)
With mItem
    .Caption = "Sub-Menu3"
    .OnAction = "submenu3"
End With

On Error GoTo 0

End Sub

因此,上面的代码创建了一个加载项功能区,其中包含“菜单”按钮和子菜单“子菜单1,子菜单2和子菜单3”。

如果我点击任何子菜单,我需要一个消息框,显示我点击的菜单名称。

1 个答案:

答案 0 :(得分:0)

Commandbars对象的ActionControl属性将告诉您单击了哪个菜单项来调用您的代码。

Dim ctl As CommandBarControl

Set ctl = Application.Commandbars.ActionControl