Workbook_Open command bar set up

nigelandrewfoster

Well-known Member
Joined
May 27, 2009
Messages
747
The following code does what it should, but I'm for elegance, and it galls me to repeat code like this:
Code:
Private Sub workbook_open()
    Dim NewItem As CommandBarControl, OldItem As CommandBarControl
    
    On Error Resume Next
    Do
    Application.CommandBars("Cell").Controls("Product By Customer").Delete
    Loop Until Err
    Do
        Application.CommandBars("Cell").Controls("Outstanding Customers").Delete
    Loop Until Err
    Err.Clear
    Do
        Application.CommandBars("Cell").Controls("Direct Input").Delete
    Loop Until Err
    Err.Clear
    Do
        Application.CommandBars("Cell").Controls("Amend Despatch Info").Delete
    Loop Until Err
    Err.Clear
    Do
        Application.CommandBars("Cell").Controls("Print Individual Delivery Note").Delete
    Loop Until Err
    Err.Clear
    On Error GoTo 0

    Set NewItem = Application.CommandBars("Cell").Controls.Add
    NewItem.Caption = "Direct Input"
    NewItem.OnAction = "Direct_Input"
    NewItem.BeginGroup = True

    Set NewItem = Application.CommandBars("Cell").Controls.Add
    NewItem.Caption = "Amend Despatch Info"
    NewItem.OnAction = "Despatch_Info_Amend"
    NewItem.BeginGroup = True

    Set NewItem = Application.CommandBars("Cell").Controls.Add
    NewItem.Caption = "Print Individual Delivery Note"
    NewItem.OnAction = "Del_Note_Indiv"
    NewItem.BeginGroup = True
    
    Set NewItem = Application.CommandBars("Cell").Controls.Add
    NewItem.Caption = "Direct Input"
    NewItem.OnAction = "Direct_Input"
    NewItem.BeginGroup = True

    Set NewItem = Application.CommandBars("Cell").Controls.Add
    NewItem.Caption = "Amend Despatch Info"
    NewItem.OnAction = "Despatch_Info_Amend"
    NewItem.BeginGroup = True

    Set NewItem = Application.CommandBars("Cell").Controls.Add
    NewItem.Caption = "Print Individual Delivery Note"
    NewItem.OnAction = "Del_Note_Indiv"
    NewItem.BeginGroup = True
    'Call RightClickMenu
End Sub
However, what I thought was going to be a simple job of storing my Menu items on a worksheet and reading off the details into string variables to then pass to a general purpose routine like this:

Code:
Sub MenuItems(menu As String, item As String, replace, onaction As String, begin As Boolean)
    Dim NewItem As CommandBarControl
    Do
        On Error Resume Next
        [COLOR="Red"]Application.CommandBars(menu).Controls(item).Delete[/COLOR]
        If Err Then Exit Do
        On Error GoTo 0
    Loop

    If replace = 1 Then
        Set NewItem = Application.CommandBars(menu).Controls.Add
        NewItem.Caption = item
        NewItem.onaction = onaction
        NewItem.BeginGroup = begin
    End If
End Sub
It fails at the red line. The line works if I replace 'menu' and 'item' with "Cells" and "Direct Input", for example. Why doesn't it accept string variables as its arguments??
Thanks very much.
Nigel
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
To delete all the custom cell command bar buttons, use:
Code:
CommandBars("Cell").Reset
then you can get rid of your Delete loop, assuming you want to delete them all.
 
Upvote 0
Thanks. I probably will use that method (which I had been warned against, in case I was using any third party add ons, whose menu items I would also be erasing b3 that method.
I still need to know why my code doesn't work when I am sending it valid values via string variagles...?
 
Upvote 0

Forum statistics

Threads
1,225,072
Messages
6,182,697
Members
453,132
Latest member
nsnodgrass73

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