Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
sjschmidtky
 
Posts: n/a
Default Workbook Specific Toolbar Menus

I was proud of myself in creating a new toolbar menu and sub-menu and
assigning macros to the menu items until I discovered that the menu appears
on the toolbar always now, regardless of the spreadsheet I open. It appears
that when I select a custom menu item, it's going out the the spreadsheet
where it is stored and executing it against the spreadsheet I have open
(great, but that's not what I want).

How do I create toolbar menus that only appear for a specific spreadsheet
when it is open?
--
Steve
  #2   Report Post  
Bernie Deitrick
 
Posts: n/a
Default

Steve,

The best option is to create the commandbar on the fly, when the workbook is
opened, and delete the commandbar when the workbook is closed.

Follow these instructions and example code.

In the workbook's Thisworkbook object code module, place the following code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
DeleteCommandbar
End Sub

Private Sub Workbook_Open()
CreateCommandbar
End Sub

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
On Error GoTo NotThere
Application.CommandBars("My Bar").Visible = True
Exit Sub
NotThe
CreateCommandbar
End Sub

Private Sub Workbook_WindowDeactivate(ByVa*l Wn As Window)
On Error Resume Next
Application.CommandBars("My Bar").Visible = False
End Sub

In a regular code module, place the following:

Dim myBar As CommandBar
Dim myButton As CommandBarButton

Sub CreateCommandbar()

On Error Resume Next
DeleteCommandBar

Set myBar = Application.CommandBars.Add("M*y Bar")
With myBar
.Position = msoBarTop
.Visible = True
.Enabled = True
Set myButton = .Controls.Add(Type:=msoControl*Button, ID:=23)
With myButton
.Caption = "Hello"
.Style = msoButtonIcon
.FaceId = 137
.Enabled = True
.OnAction = "SayHello"
End With
End With

End Sub

Sub DeleteCommandBar()
'Delete the commandbar if it already exists
On Error Resume Next
Application.CommandBars("My Bar").Delete
End Sub

Sub SayHello()
MsgBox "Hello there"
End Sub

You can add as many buttons or other menu items as you like.

HTH,
Bernie
MS Excel MVP



"sjschmidtky" wrote in message
...
I was proud of myself in creating a new toolbar menu and sub-menu and
assigning macros to the menu items until I discovered that the menu appears
on the toolbar always now, regardless of the spreadsheet I open. It appears
that when I select a custom menu item, it's going out the the spreadsheet
where it is stored and executing it against the spreadsheet I have open
(great, but that's not what I want).

How do I create toolbar menus that only appear for a specific spreadsheet
when it is open?
--
Steve



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Toolbar problem Anthony Excel Discussion (Misc queries) 1 June 1st 05 01:05 AM
Extract specific data into its own workbook via macro? Adrian B Excel Discussion (Misc queries) 2 February 24th 05 06:09 AM
Copying a workbook with custom toolbar assigned to a macro Matt W Excel Discussion (Misc queries) 1 February 4th 05 10:46 PM
Unprotect Workbook Kent Excel Discussion (Misc queries) 1 February 4th 05 01:07 AM
How to open a workbook with a specific worksheet displayed by defa ThaiNB New Users to Excel 2 January 21st 05 06:11 AM


All times are GMT +1. The time now is 10:53 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"