Custom VBA Menu gets disabled after runtime error 91

EnergyStar

New Member
Joined
Jun 10, 2014
Messages
1
Hello,

Thanks for viewing my thread. I am troubleshooting a macro enabled spreadsheet which adds a custom menu through vba. It does not have an Add-in but adds the menu before Help, so the custom menu appears under Add-Ins. The custom menu is dynamically created on workbook_open() and consists of a popup menu that includes two controls and three popup submenus.

The spreadsheet worked fine until I opened another instance of the same spreadsheet, resulting in a runtime 91 error.
The custom menu did not display properly after that. The three popup submenus are greyed out whenever the spreadsheet is opened, while the other two are unaffected.

here is the bit of code that enables the popup submenus depending on the worksheet tag. I have stepped through the code and verified that the menus are set to enabled and visible, yet they do not display on the menu bar.
Code:
 If (Application.ActiveSheet.CodeName = "SomeName") Then
            Application.CommandBars.FindControl(Tag:="SomeNameTag").Enabled = True
          If (Application.ActiveSheet.CodeName = "SomeOtherName") Then
            Application.CommandBars.FindControl(Tag:="SomeOtherNameTag").Enabled = True

I then looked for the control using the below code and did not find them.

Code:
Dim objCommandBar1 As Office.CommandBar
Dim objControl As Office.CommandBarControl

    For Each objCommandBar In CommandBars
    Set objControl = objCommandBar.FindControl(Tag:="SomeNameTag", Recursive:=True)

       If Not (objControl Is Nothing) Then
           Debug.Print objCommandBar.Name
       End If
     
    Next objCommandBar

However when I tried the following code, I was able to find the commandbar objects and deleted them.

Code:
Dim myPopupControls As Office.CommandBarControls
        Dim PopupCtl As Office.CommandBarControl
        
        Set myPopupControls = Application.CommandBars.FindControls(Tag:=s_PopupMenuTag)
        
        If Not (myPopupControls Is Nothing) Then
            For Each PopupCtl In myPopupControls
                If Not (PopupCtl Is Nothing) Then
                      '  Debug.Print PopupCtl.Name
                      '  Debug.Print PopupCtl.ID
                      '  Debug.Print PopupCtl.Enabled
                      '  Debug.Print PopupCtl.Visible
                      PopupCtl.Delete
                End If
            Next PopupCtl
        End If

My question:
Those controls were being created and enabled somewhere whenever the spreadsheet was run, and I would like to find out where. Can someone throw some light on what was happening with the spreadsheet and how I may re-enable the menu without including the code to first delete the controls created.

Thanks and any help is appreciated.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,222,786
Messages
6,168,220
Members
452,171
Latest member
saeid025

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