Adding sub-menu groups to contect menu

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
Office Version
  1. 365
Platform
  1. Windows
I have code that I've used for yonks to create a custom right-click context menu. It's getting long and I'm wondering how to create sub menus to group related macros. Here's my code:

VBA Code:
Private Sub Workbook_Open()

Dim contextMenu As CommandBar

Set contextMenu = Application.CommandBars("Cell")

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=30)
     .OnAction = "GeneralComment"
     .FaceId = 31
     .Caption = "Add Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=31)
     .OnAction = "DeleteGeneralComment"
     .FaceId = 31
     .Caption = "Delete Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=32)
     .OnAction = "OptionComment"
     .FaceId = 31
     .Caption = "Add Options Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=33)
     .OnAction = "DeleteOptionsComment"
     .FaceId = 31
     .Caption = "Delete Options Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=34)
     .OnAction = "AddComment"
     .FaceId = 31
     .Caption = "Sent to CC Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=35)
     .OnAction = "AddCommentReturn"
     .FaceId = 31
     .Caption = "Returned back from CC Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=36)
     .OnAction = "DeleteComment"
     .FaceId = 31
     .Caption = "Delete Sent to CC Comment"
     .Tag = "My_Cell_Control_Tag"
End With

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Perhaps something like this ...

VBA Code:
Private Sub Workbook_Open()

    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        With .Add(Type:=msoControlButton, Before:=30)
            .OnAction = "GeneralComment"
            .FaceId = 31
            .Caption = "Add Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
    
        With .Add(Type:=msoControlButton, Before:=31)
            .OnAction = "DeleteGeneralComment"
            .FaceId = 31
            .Caption = "Delete Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<

    End With


    With myPopUp.Controls                                   ' <<<<<<
        With .Add(Type:=msoControlButton)
            .OnAction = "OptionComment"
            .FaceId = 31
            .Caption = "Add Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        With .Add(Type:=msoControlButton)
            .OnAction = "DeleteOptionsComment"
            .FaceId = 31
            .Caption = "Delete Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With

End Sub
 
Upvote 0
Perhaps something like this ...

VBA Code:
Private Sub Workbook_Open()

    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        With .Add(Type:=msoControlButton, Before:=30)
            .OnAction = "GeneralComment"
            .FaceId = 31
            .Caption = "Add Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
   
        With .Add(Type:=msoControlButton, Before:=31)
            .OnAction = "DeleteGeneralComment"
            .FaceId = 31
            .Caption = "Delete Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<

    End With


    With myPopUp.Controls                                   ' <<<<<<
        With .Add(Type:=msoControlButton)
            .OnAction = "OptionComment"
            .FaceId = 31
            .Caption = "Add Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        With .Add(Type:=msoControlButton)
            .OnAction = "DeleteOptionsComment"
            .FaceId = 31
            .Caption = "Delete Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With

End Sub

This works nicely. Is it possible to have a caption for the sub menu? With your code it's just blank with an arrow.
 
Upvote 0
I could have been a little more elaborate. So try ...

VBA Code:
    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<
        With myPopUp
            .Caption "All About Comments"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With
 
Upvote 0
Solution
I could have been a little more elaborate. So try ...

VBA Code:
    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<
        With myPopUp
            .Caption "All About Comments"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With

Perfect. Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top