Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't Disable Excel Custom ToolBar On Close

Status
Not open for further replies.

TheAceMan1

Programmer
Sep 23, 2003
11,174
US
Howdy All . . .

Platform: Vista Home Premium, Excel 2003 SP3

Have a custom toolbar that I'd like to disable on close (so it doesn't appear globally). Using the [blue]Workbook_BeforeClose[/blue] or [blue]Workbook_Deactivate[/blue] events produces the following error:
[blue]Run-Time error '91':

Object variable or With block not set[/blue]
... and the code that produces the error:
Code:
[blue]Private Sub Workbook_BeforeClose(Cancel As Boolean)
   CommandBars("STS1").Enabled = False
End Sub

[green]'or[/green]

Private Sub Workbook_Deactivate(Cancel As Boolean)
   CommandBars("STS1").Enabled = False
End Sub[/blue]
If I set an object 1st, as in:
Code:
[blue]   Dim CB As CommandBar
   
   [COLOR=red yellow]Set CB = CommandBars("STS1")[/color]
   
   CB.Enabled = False
   
   Set CB = Nothing[/blue]
... the same error occurs on the highlighted line. I believe the [blue]Office11[/blue] reference is no longer available at this point.

Any Ideas on disabling the custom toolbar on close?

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
To All . . .

Problem solved! [thumbsup2]

I had to wrap the code in a function:
Code:
[blue]Public Function DisableCustomCBar()
   CommandBars("STS1").Enabled = False
End Function[/blue]
Then from the workbooks [blue]BeforeClose[/blue] event, its:
Code:
[blue]Private Sub Workbook_BeforeClose(Cancel As Boolean)
   [purple][b]Run[/b][/purple] DisableCustomCBar
End Sub[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top