Closing all toolbars upon opening a workbook

Todd_M

Board Regular
Joined
Feb 24, 2002
Messages
117
Hi-
Im using the following code to hide all toolbars in the openworkbook event handler upon opening a workbook. It doesn't do anything.



Private Sub Workbook_Open()
Dim tb As CommandBar
Dim tbnum As Integer
Dim tbsheet As Worksheet
Set tbsheet = Sheets("tbsheet")
Application.ScreenUpdating = False
tbsheet.Cells.Clear
tbnum = 0
For Each tb In CommandBars
If tb.Type = msoBarTypeNormal Then
If tb.Visible Then
tbnum = tbnum + 1
tb.Visible = False
tbsheet.Cells(tbnum, 1) = tb.Name
End If
End If
Next tb
Application.ScreenUpdating = True

End Sub



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim tbsheet As Worksheet
Set tbsheet = Sheets("tbsheet")
Application.ScreenUpdating = False
On Error Resume Next
For Each cell In tbsheet.Range("A:A").SpecialCells(xlCellTypeConstants)
CommandBars(cell.Value).Visible = True
Next cell
Application.ScreenUpdating = True
End Sub


Unless I add a sheet called "tbsheet", I get a error saying "tbsheet" is out of range. When I add a sheet with the name, the workbook opens with the tool bars still showing.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
To answer the first question, to make the toolbars invisible, all you need to do is something like this:

<pre>
Dim tb As CommandBar

For Each tb In CommandBars
tb.Enabled = False
Next</pre>


This is instead of using the "Visible" method. The reason it craps out unless you've got that worksheetis because your code is trying to write all of the names of the visible toolbars to that worksheet so that the application can be reset when you are closing. So if the code is trying to reference this sheet and it's not there, it doesn't work. I recommend keeping that sheet in the work book.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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