Okay, I'm stumped - On Menu creation

crisskw

New Member
Joined
Jun 4, 2005
Messages
6
I am calling this module from the Workbook_Open event. I get the compile error "A module is not a valid type" on "Dim cBarParentBar As CommandBar". I have tried this before with success, although it was in a different workbook.
Code:
Sub SetUpCommandBar()
    Dim cBarParentBar As CommandBar
    Dim cBarParentBarPopUp As CommandBarPopup
    Dim cBarParentBarPopUpControls(1 To 5) As CommandBarButton
    Dim intArrayCounter As Integer
    
    Set cBarParentBar = Application.CommandBars("Worksheet Menu Bar")

    On Error Resume Next
    cBarParentBar.Controls("Excel to CSV").Delete
    On Error GoTo 0

    Set cBarParentBarPopUp = cBarParentBar.Controls.Add( _
        msoControlPopup, , , , True)

    With cBarParentBarPopUp
        .Caption = "Excel to CSV"
        .Visible = True
    End With

    For intArrayCounter = 1 To 5
        Set cBarParentBarPopUpControls(intArrayCounter) = cBarParentBarPopUp.Controls.Add( _
            msoControlButton, , , , True)

        With cBarParentBarPopUpControls(intArrayCounter)
            .Caption = "Add button # " & intArrayCounter
            .Style = msoButtonCaption
            .Visible = True
            .OnAction = "CommandBarButton_Click"
            .Tag = intArrayCounter
        End With
    Next

End Sub

I would appreciate any suggestions. I have looked at this long enough...
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Okay, now that I have had some sleep, I can see my error, I inadvertantly named another module CommandBar to some silly reason. I changed that name and now it runs. However, another question. I know how to delete my custom menu on WorkBook_BeforeClose, but is there a good way to check and see if the menu is present before deleting it?
 
Upvote 0
try this (just in case there are multiple menus with the same name):

Code:
    MenuName = "&MyMenu"
    On Error Resume Next
    Do While Err = 0
        CommandBars("Worksheet Menu Bar").Controls(MenuName).Delete
    Loop
    On Error GoTo 0
 
Upvote 0
Hi [Select a UserName]

The simplest approach is to enclose your deletion line between:


On Error Resume Next

and

On Error GoTo 0
 
Upvote 0
Thanks. That's the approach I was taking, but wasn't sure that it was the best approach.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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