Massimo Scola
asked on
Delete commandbar in Excel 2003 with VBA
Hello everyone
I've created a custom commandbar. I wrote following code to delete it once the workbook has been closed:
When I close the workbook (within Excel) and then open a new file, the toolbar is still visible. Strangly, when I open the same file again, I end up having two commandbars.
Any idea what I am doing wrong?
Thanks
Massimo
I've created a custom commandbar. I wrote following code to delete it once the workbook has been closed:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
Application.CommandBars("Worksheet Menu Bar").Controls("My Macros").Delete 'delete the menu item
End Sub
When I close the workbook (within Excel) and then open a new file, the toolbar is still visible. Strangly, when I open the same file again, I end up having two commandbars.
Any idea what I am doing wrong?
Thanks
Massimo
You presumably added the commandbar with a appliaction.commandbars.ad d command and if so then try:
Application.CommandBars("M y Macros").Delete
Chris
Application.CommandBars("M
Chris
ASKER
Sidd: Yes, it is in Workbook_Open. Here a part of the code:
Shouldn't it be in Workbook_Open() ?
Massimo
Dim cmbBar As CommandBar
Dim cmbControl As CommandBarControl
Set cmbBar = Application.CommandBars("Worksheet Menu Bar")
Set cmbControl = cmbBar.Controls.Add(Type:=msoControlPopup, temporary:=True) 'adds a menu item to the Menu Bar
Shouldn't it be in Workbook_Open() ?
Massimo
There is one line missing after line 5?
cmbControl.Caption = "My Macros"
Now try it.
Sid
cmbControl.Caption = "My Macros"
Now try it.
Sid
Sorry missed your question.
>> Shouldn't it be in Workbook_Open() ?
It should. :)
I believe since you didn't set the caption so on close it was not deleting the commandbar.
Sid
>> Shouldn't it be in Workbook_Open() ?
It should. :)
I believe since you didn't set the caption so on close it was not deleting the commandbar.
Sid
ASKER
Sid, I tried it but the menus/commandbar do not go away when I close the file.
Here is the entire code
strange, isn't it?
Here is the entire code
Private Sub Workbook_Open()
Dim cmbBar As CommandBar
Dim cmbControl As CommandBarControl
Set cmbBar = Application.CommandBars("Worksheet Menu Bar")
Set cmbControl = cmbBar.Controls.Add(Type:=msoControlPopup, temporary:=True) 'adds a menu item to the Menu Bar
cmbControl.Caption = "My Macros"
With cmbControl
.Caption = "&IG Arbeit Logistik" 'names the menu item
With .Controls.Add(Type:=msoControlButton) 'adds a dropdown button to the menu item
.Caption = "Aufträge Shopping Taxi" 'adds a description to the menu item
.OnAction = "AufträgeShoppingTaxi" 'runs the specified macro
.FaceId = 1098 'assigns an icon to the dropdown
End With
With .Controls.Add(Type:=msoControlButton)
.Caption = "Abonnement Typ"
.OnAction = "Abos"
.FaceId = 7098
End With
End With
Sheets("Start").Activate
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
Application.CommandBars("Worksheet Menu Bar").Controls("My Macros").Delete 'delete the menu item
End Sub
strange, isn't it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Basically the commandbar was never called "my Macros" and that is why it's deletion was not working.
Delelting your original with that in my previous post will hopefully work.
Chris
Delelting your original with that in my previous post will hopefully work.
Chris
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
Application.CommandBars("Worksheet Menu Bar").Controls("&IG Arbeit Logistik").Delete 'delete the menu item
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ah... right!
Thanks a lot!
Massimo
Thanks a lot!
Massimo
ASKER
thanks
Are you recreating the control there?
Sid