|
|
|
|
|
|
Excel
|
Using workbook events to control the visibility of defined toolbars/menus
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
2002, 2003
|
Submitted by:
|
Killian
|
Description:
|
This example uses the workbook Activate event to force the display of the workbook's custom toolbar (hiding the other Excel toolbars) and the Deatcivate event to restore the user's original toolbar layout.
|
Discussion:
|
When distrubuting custom workbooks/automation that have specific functionality, it is often helpful for the user if unessecary toolbars are hidden and just the custom toolbar is left showing.
It is obviously vital that when the distributed workbook is closed or made inactive, the user's original toolbar layout is restored. This example saves the user's visible toolbar names on a hidden worksheet and then hides them when the workbook is activated.
When it's deactivated, the values on the hidden sheet are used to retore the original toolbar states.
|
Code:
|
instructions for use
|
Option Explicit
Sub BuildToolBar()
Dim Tbar As CommandBar
Dim btnNew As CommandBarControl
Dim i As Long
DeleteToolbar
Set Tbar = CommandBars.Add(Name:="My XL App Toolbar")
Tbar.Visible = True
For i = 1 To 8
Set btnNew = Tbar.Controls.Add(Type:=msoControlButton)
With btnNew
.Caption = "Button " & i
.Style = msoButtonIconAndWrapCaptionBelow
.OnAction = "DefaultAction"
.FaceId = 480 + i
.Width = 60
End With
Next i
Tbar.Position = msoBarTop
Tbar.Left = 0
Tbar.Protection = msoBarNoMove
Set btnNew = Nothing
Set Tbar = Nothing
End Sub
Sub DeleteToolbar()
Dim cb As CommandBar
For Each cb In CommandBars
If cb.Name = "My XL App Toolbar" Then
cb.Delete
End If
Next cb
End Sub
Sub DefaultAction()
MsgBox CommandBars.ActionControl.Caption & " was pressed."
End Sub
Option Explicit
Private Sub Workbook_Open()
BuildToolBar
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
DeleteToolbar
End Sub
Private Sub Workbook_Activate()
Dim Tbar As CommandBar
Dim count As Integer
Dim wsMenu As Worksheet
Application.ScreenUpdating = False
Set wsMenu = ThisWorkbook.Sheets("Menu")
wsMenu.Cells.Clear
count = 0
For Each Tbar In Application.CommandBars
If Tbar.Type = msoBarTypeNormal Then
If Tbar.Visible Then
count = count + 1
Tbar.Visible = False
wsMenu.Cells(count, 1) = Tbar.Name
End If
End If
Next Tbar
Application.CommandBars("My XL App Toolbar").Visible = True
Application.ScreenUpdating = True
Set wsMenu = Nothing
End Sub
Private Sub Workbook_Deactivate()
Dim c As Range
Dim wsMenu As Worksheet
Application.ScreenUpdating = False
Set wsMenu = ThisWorkbook.Sheets("Menu")
On Error Resume Next
For Each c In wsMenu.Range("A:A") _
.SpecialCells(xlCellTypeConstants)
Application.CommandBars(c.Value).Visible = True
Next c
Application.CommandBars("My XL App Toolbar").Visible = False
Application.ScreenUpdating = True
Set wsMenu = Nothing
End Sub
|
How to use:
|
- Open the VBE in a new workbook
- Add a standard module (Insert>Module) and paste in the code (up to "code for standard module - END")
- Go to the WorkBook code module (Double click the "ThisWorkbook" item in the Project Explorer
- Paste in the code for the workbook events
- Rename a sheet in the workbook "Menu"
- In the Project Explorer, select the renamed sheet (e.g "Sheet1 (Menu)") and set its "Visible" property to 0 (xlSheetHidden)
- Save and close the workbook
|
Test the code:
|
- When th project workbook is active, the user should only have the default menu (Worksheet Menu Bar) and the custom toolbar, docked at the top and locked.
- When switching to other workbooks, the custom tolbar should be hidden and the users original toolbars restored.
|
Sample File:
|
CustomAppToolBar.zip 13.67KB
|
Approved by mdmackillop
|
This entry has been viewed 229 times.
|
|