+ Reply to Thread
Results 1 to 2 of 2

How Do I Add Add More Menus In A Pupup Menu?

Hybrid View

  1. #1
    Registered User
    Join Date
    08-04-2015
    Location
    Manchester, England
    MS-Off Ver
    Office 2013
    Posts
    6

    How Do I Add Add More Menus In A Pupup Menu?

    I have made a context menu in Excel which works great. I am just unsure how to add another menu after .Caption = "My Special Menu"

    So I would like the context menu to appear like this.

    My Special Menu > IT > Microsoft Office (then the 2 buttons)

    Is this possible in a context menu?

    Also is it possible to have another menu under Microsoft Office when you press IT to say Google Docs with buttons in those also?

    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)
    
            ' First, add two buttons to the menu.
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 1"
                .FaceId = 71
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
            End With
    
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 2"
                .FaceId = 72
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
            End With
    
            ' Next, 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 & "'!" & "TestMacro"
                End With
    
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Button 2 in menu"
                    .FaceId = 72
                    .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
                End With
            End With
    
            ' Finally, add a single button.
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 3"
                .FaceId = 73
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
            End With
    
        End With
    End Sub
    
    Sub TestMacro()
        MsgBox "Hi there!"
    End Sub

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,642

    Re: How Do I Add Add More Menus In A Pupup Menu?

    Hi there,

    See if the following code does what you need:

    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Public Const msPOPUP_NAME As String = "MyPopUpMenu"
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub DeletePopUpMenu()
    
    '   Delete the popup menu if it already exists.
        On Error Resume Next
            Application.CommandBars(msPOPUP_NAME).Delete
        On Error GoTo 0
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub CreateAndDisplayPopUpMenu()
    
    '   Delete any existing popup menu.
        Call DeletePopUpMenu
    
    '   Create the popup menu.
        Call CreatePopUpMenu
    
    '   Display the popup menu.
        On Error Resume Next
            Application.CommandBars(msPOPUP_NAME).ShowPopup
        On Error GoTo 0
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub CreatePopUpMenu()
    
        Dim Menu_Special    As CommandBarPopup
        Dim Menu_Office     As CommandBarPopup
        Dim Menu_Google     As CommandBarPopup
        Dim Menu_IT         As CommandBarPopup
    
    '   Add the popup menu.
        With Application.CommandBars.Add(Name:=msPOPUP_NAME, Position:=msoBarPopup, _
                                         MenuBar:=False, Temporary:=True)
    
    '       First, add two buttons to the menu.
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 1"
                .FaceId = 71
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
            End With
    
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 2"
                .FaceId = 72
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
            End With
    
    '       Next, add a menu that contains the IT sub-menu.
            Set Menu_Special = .Controls.Add(Type:=msoControlPopup)
            With Menu_Special
    
                .Caption = "My Special Menu"
    
    '           Add the IT sub-menu which will in turn contain two further sub-menus
                Set Menu_IT = .Controls.Add(Type:=msoControlPopup)
                With Menu_IT
    
                    .Caption = "IT"
    
    '               Add the Microsoft Office sub-menu which will contain two control buttons
                    Set Menu_Office = .Controls.Add(Type:=msoControlPopup)
    
                    With Menu_Office
    
                        .Caption = "Microsoft Office"
    
    '                   Add the control buttons
                        With .Controls.Add(Type:=msoControlButton)
                            .Caption = "Office - Button 1"
                            .FaceId = 71
                            .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
                        End With
    
                        With .Controls.Add(Type:=msoControlButton)
                            .Caption = "Office - Button 2"
                            .FaceId = 72
                            .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
                        End With
    
                    End With
    
    '               Add the Google Docs sub-menu which will contain two control buttons
                    Set Menu_Google = .Controls.Add(Type:=msoControlPopup)
    
                    With Menu_Google
    
                        .Caption = "Google Docs"
    
    '                   Add the control buttons
                        With .Controls.Add(Type:=msoControlButton)
                            .Caption = "Google - Button 1"
                            .FaceId = 71
                            .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
                        End With
    
                        With .Controls.Add(Type:=msoControlButton)
                            .Caption = "Google - Button 2"
                            .FaceId = 72
                            .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
                        End With
    
                    End With
    
                End With
    
            End With
    
    '       Finally, add a single button to the original (top-level) menu.
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Button 3"
                .FaceId = 73
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
            End With
    
        End With
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub TestMacro()
        MsgBox "Hi there!"
    End Sub
    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. omit menu items via data validation for multiple selections and multiple menus when select
    By themattyp503 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-03-2014, 12:16 AM
  2. How to Add a pop up menus with multiple menus levels
    By elbertvillarreal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-23-2012, 12:36 PM
  3. Replies: 0
    Last Post: 11-27-2010, 06:38 AM
  4. Replies: 3
    Last Post: 12-10-2009, 09:30 PM
  5. [SOLVED] New Menus - attaching but menus are reset
    By Greegan in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-05-2005, 11:25 AM
  6. [SOLVED] save and restore "Workbook Menu Bar" & "Cell" menus
    By Jeff Higgins in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2005, 10:08 PM
  7. Shortcut Pupup Menu?
    By Ricky S in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2005, 11:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1