Excel VBA - adding submenus to user context menu

For a long time, the viewer, the first time a poster. I have a form with Right-Click functions that work fine. I am trying to add a submenu to the main menu of the right mouse button to separate some functions / command. I need / want to insert a section where "Select Case", however, is displayed only in the top menu. Not sure where to go from here. Any help would be awesome

Thanks:)

PS I would be happy to explain if necessary.

Sub fzCopyPaste(iItems As Integer) On Error Resume Next CommandBars("Custom").Delete Set PopBar = CommandBars.Add(Name:="Custom", Position:=msoBarPopup, MenuBar:=False, Temporary:=True) 

'Add top_menu to the main panel: this works great

 Set top_menu = PopBar.Controls.Add(Type:=msoControlButton) With top_menu '.FaceId = .Caption = "&Some Commands" End With 

You need to insert the following submenus into the top menu But nothing appears: does not work

 Select Case iItems Case 1 ' Copy and Paste Set copy_button = top_menu.Controls.Add(Type:=msoControlButton) With copy_button .FaceId = 19 .Caption = "&Copy" .Tag = "tCopy" .OnAction = "fzCopyOne(true)" End With Set paste_button = top_menu.Controls.Add(Type:=msoControlButton) With paste_button .FaceId = 22 .Tag = "tPaste" .Caption = "&Paste" .OnAction = "fzCopyOne(true)" End With Case 2 ' Paste Only Set paste_button = top_menu.Controls.Add(Type:=msoControlButton) With paste_button .FaceId = 22 .Tag = "tPaste" .Caption = "&Paste" .OnAction = "fzCopyOne(true)" End With End Select 

'Additional top menus below: This work perfectly

  Set paste_button = PopBar.Controls.Add(Type:=msoControlButton) With paste_button .FaceId = 22 .Tag = "tPaste" .Caption = "Main POP BAR 2" .OnAction = "fzCopyOne(true)" End With PopBar.ShowPopup CommandBars("Custom").Delete End Sub 
+5
source share
1 answer

You set Copy_Button to msoControlButton . If you want a button, that’s right. You want a menu, but you must set it to msoControlPopup . Try something like this:

 Set Top_Menu = PopBar.Controls.Add(Type:=msoControlPopup) With Top_Menu .Caption = "&Some Commands" Set MySubMenu = .Controls.Add(Type:=msoControlPopup, before:=1, temporary:=True) Select Case iItems Case 1 With MySubMenu .Caption = "Submenu Commands" With .Controls.Add(Type:=msoControlButton, before:=1, temporary:=True) .FaceId = 19 .Caption = "&Copy" .Tag = "tCopy" .OnAction = "fzCopyOne(true)" End With With .Controls.Add(Type:=msoControlButton, before:=2, temporary:=True) .FaceId = 22 .Tag = "tPaste" .Caption = "&Paste" .OnAction = "fzCopyOne(true)" End With End With Case 2 'etc End Select End With 

I deleted the "Top_Menu" section (first 3 lines) with the following; he added an additional button, and then the desired menu.

 Set MySubMenu = PopBar.Controls.Add(Type:=msoControlPopup, before:=1, temporary:=True) With MySubMenu .Caption = "&Some Commands" 
+2
source

All Articles