Method 'Delete' of object '-CommandBarButton' failed

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I have the following code in "ThisWorkbook" module.

Rich (BB code):
Private Sub Workbook_Open()
    Dim ContextMenu As CommandBar
    Dim ctrl As CommandBarControl
'Delete the control first to avoid duplicates
    Set ContextMenu = Application.CommandBars("Cell")

'Delete custom controls with the Tag : My_Cell_Control_Tag
    For Each ctrl In ContextMenu.Controls
        If ctrl.Tag = "My_Cell_Control_Tag" Then
            ctrl.Delete
        End If
    Next ctrl

'Set ContextMenu to the Cell menu
    Set ContextMenu = Application.CommandBars("Cell")

'Add one custom button to the Cell menu
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
        .OnAction = "'" & ThisWorkbook.Name & "'!CreateDisplayPopUpMenu"
        .FaceId = 59
        .Caption = "Create Reports"
        .Tag = "My_Cell_Control_Tag"
    End With
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ContextMenu As CommandBar
    Dim ctrl As CommandBarControl

    'Set ContextMenu to the Cell menu
    Set ContextMenu = Application.CommandBars("Cell")

    'Delete custom controls with the Tag : My_Cell_Control_Tag
    For Each ctrl In ContextMenu.Controls
        If ctrl.Tag = "My_Cell_Control_Tag" Then
            ctrl.Delete
        End If
    Next ctrl
    
    On Error Resume Next
        Application.CommandBars(Mname).Delete
    On Error GoTo 0
End Sub
I get the "Delete' of object '-CommandBarButton' failed" error when I utilize Windows(ThisWorkbook.Name).Close from Module1...but not if I manually close the file.

What am I missing? :confused:
 
Last edited:

Some videos you may like

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)

Watch MrExcel Video

Forum statistics

Threads
1,122,222
Messages
5,594,904
Members
413,952
Latest member
JGer

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
Top