How to check if a toolbar exists before creating or deleting it

S

Seeb

Hi everybody,

I'd like to know if there's a way to check if a temporary Toolbar is present
before deleting it. I have a document that must create a toolbar on the
DocumentOpened event and deletes it on the BeforeDocumentClose Event. I
tried this:

Sub DeleteDrawingCommandBar()

Dim cbars As Office.CommandBars
Dim cbar1 As Office.CommandBar

On Error Resume Next

Set cbars = Application.CommandBars

If (cbars("MyToolbar").Enabled = True) Then 'BUG HERE
Set cbar1 = cbars("MyToolbar")
cbar1.Delete
End If

end sub

But it fails at the line 'BUG HERE if MyToolbar doesn't exist. Is there a
way to check if the toolbar is present before deleting it, and in the same
way checking if it exists before creating it?

Thanks a lot,

Seeb
 
J

Jon Summers

Seeb ...
I'd like to know if there's a way to check if a temporary Toolbar is
present
before deleting it. Function CBDeleteCommandBar(strCBarName As String)

Fresh from the MSDN documentation:

Function CBDeleteCommandBar(strCBarName As String) As Boolean
On Error Resume Next
Application.CommandBars(strCBarName).Delete
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top