I'm using this code to load a context menu from an addin.
It works, except that when the addin is deselected, the context menu still shows.
The menu consists of msoControlButton and msoBarPopup.
WORKBOOK
It works, except that when the addin is deselected, the context menu still shows.
The menu consists of msoControlButton and msoBarPopup.
WORKBOOK
VBA Code:
Option Explicit
Private Sub Workbook_Open()
Call cell_menu_normal
Call cell_menu_page_break_preview
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call del_cell_menu_normal
Call del_cell_menu_page_break_preview
Call del_popup
End Sub
VBA Code:
Option Explicit
Option Private Module
Dim context_menu As CommandBar
Dim my_menu_item As CommandBarControl
Dim my_menu_item_Popup As CommandBarPopup
Public Const my_name As String = "my_ctrl_tag"
Sub cell_menu_normal()
Call del_cell_menu_normal 'delete controls to avoid duplicates
Set context_menu = Application.CommandBars("Cell") 'Case 1: Normal
'LEVEL 1
'=======
With context_menu.Controls.Add(Type:=msoControlButton, before:=1) 'Add custom button to Cell menu
.Caption = "Temporary"
.OnAction = "'" & ThisWorkbook.Name & "'!" & "create_temp_popup_menu"
.Tag = "my_ctrl_tag"
End With
End Sub
Sub cell_menu_page_break_preview()
Call del_cell_menu_page_break_preview
Set context_menu = Application.CommandBars(Application.CommandBars("Cell").Index + 3) 'Case 2: Page Break Preview
'LEVEL 1
'=======
With context_menu.Controls.Add(Type:=msoControlButton, before:=1) 'Add custom button to Cell menu
.Caption = "Temporary": .FaceId = 7869: .OnAction = "'" & ThisWorkbook.Name & "'!" & "create_temp_popup_menu"
.Tag = "my_ctrl_tag"
End With
End Sub
Sub create_temp_popup_menu()
Call del_popup
Call CustomPopUpMenu
On Error Resume Next
Application.CommandBars(my_name).ShowPopup
On Error GoTo 0
End Sub
Sub CustomPopUpMenu()
With Application.CommandBars.Add(Name:=my_name, Position:=msoBarPopup, MenuBar:=False, Temporary:=True)
'LEVEL 2
'-------
With .Controls.Add(Type:=msoControlButton)
.Caption = "Form": .FaceId = 7974
.OnAction = "'" & ThisWorkbook.Name & "'!" & "Form"
.Tag = "my_ctrl_tag"
End With
'LEVEL 2
'-------
Set my_menu_item = .Controls.Add(Type:=msoControlPopup) 'PopUp (Level 2)
With my_menu_item
.Caption = "Range"
'LEVEL 3
With .Controls.Add(Type:=msoControlButton)
.Caption = "Copy"
.OnAction = "'" & ThisWorkbook.Name & "'!" & "Copy"
.Tag = "my_ctrl_tag"
End With
'LEVEL 3
With .Controls.Add(Type:=msoControlButton)
.Caption = "Paste"
.OnAction = "'" & ThisWorkbook.Name & "'!" & "Paste"
.Tag = "my_ctrl_tag"
End With
End With
End With
End Sub
Sub del_cell_menu_normal()
Set context_menu = Application.CommandBars("Cell") 'Case 1: Normal
For Each my_menu_item In context_menu.Controls
If my_menu_item.Tag = "my_ctrl_tag" Then
my_menu_item.Delete
End If
Next my_menu_item
End Sub
Sub del_cell_menu_page_break_preview()
Set context_menu = Application.CommandBars(Application.CommandBars("Cell").Index + 3) 'Case 2: Page Break Preview
For Each my_menu_item In context_menu.Controls
If my_menu_item.Tag = "my_ctrl_tag" Then
my_menu_item.Delete
End If
Next my_menu_item
End Sub
Sub del_popup()
On Error Resume Next
Application.CommandBars(my_name).Delete
On Error GoTo 0
End Sub