如何在上下文菜单中添加更多菜单?

时间:2015-10-01 17:24:26

标签: excel-vba contextmenu excel-2013 vba excel

我在Excel中创建了一个上下文菜单,效果很好。我只是不确定如何在.Caption = "My Special Menu"

之后添加另一个菜单

所以我希望上下文菜单看起来像这样。

我的特别菜单> IT> Microsoft Office(然后是2个按钮)

这可以在上下文菜单中使用吗?

当您按IT时,可以在Microsoft Office下设置另一个菜单,并在其中显示带有按钮的Google文档吗?

Option Explicit

Public Const Mname As String = "MyPopUpMenu"

Sub DeletePopUpMenu()
    ' Delete the popup menu if it already exists.
    On Error Resume Next
    Application.CommandBars(Mname).Delete
    On Error GoTo 0
End Sub

Sub CreateDisplayPopUpMenu()
    ' Delete any existing popup menu.
    Call DeletePopUpMenu

    ' Create the popup menu.
    Call Custom_PopUpMenu_1

    ' Display the popup menu.
    On Error Resume Next
    Application.CommandBars(Mname).ShowPopup
    On Error GoTo 0
End Sub

Sub Custom_PopUpMenu_1()
    Dim MenuItem As CommandBarPopup
    ' Add the popup menu.
    With Application.CommandBars.Add(Name:=Mname, Position:=msoBarPopup, _
         MenuBar:=False, Temporary:=True)

        ' add a menu that contains two buttons.
        Set MenuItem = .Controls.Add(Type:=msoControlPopup)
        With MenuItem
            .Caption = "My Special Menu"

            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 1 in menu"
                .FaceId = 71
                .OnAction = "'" & ThisWorkbook.Name & "'!" & ""
            End With

            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 2 in menu"
                .FaceId = 72
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "T"
            End With
        End With

1 个答案:

答案 0 :(得分:0)

试试这个

       With MenuItem
        .Caption = "My Special Menu"
        With .Controls.Add(Type:=msoControlPopup, before:=1)
            .Tag = "submenu1"
            .Caption = "IT"
            With .Controls.Add(Type:=msoControlPopup, before:=1)
                .Tag = "Submenu2"
                .Caption = "Microsoft Office!"


                With .Controls.Add(Type:=msoControlButton, before:=1)
                        .Tag = "btn1"
                        .Caption = "Button 1 in menu"
                        .FaceId = 71
                        .OnAction = "'" & ThisWorkbook.Name & "'!" & ""
                End With
                With .Controls.Add(Type:=msoControlButton)
                        .Tag = "btn2"
                        .Caption = "Button 2 in menu"
                        .FaceId = 72
                        .OnAction = "'" & ThisWorkbook.Name & "'!" & "T"
                End With

            End With
        End With
      End With