Context menu - from Addin

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
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
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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Have you tried placing the resetting code in the Workbook_AddinUninstall event handler instead of Workbook_BeforeClose ?
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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