Add this bit of code To the Thisworkbook module:
Option Explicit
Private Sub Workbook_Open()
AddNewToolBar
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
DeleteToolbar
End Sub
....................................................................................................
Add all of the following code To a standard module:
Option Explicit
Sub AddNewToolBar()
' This procedure creates a new temporary toolbar.
Dim ComBar As CommandBar, ComBarContrl As CommandBarControl
On Error Goto ErrorHandler
' Create a new floating toolbar and make it visible.
On Error Resume Next
'Delete the toolbar if it already exists
CommandBars("My Toolbar").Delete
Set ComBar = CommandBars.Add(Name:="My Toolbar", Position:= _
msoBarFloating, Temporary:=True)
ComBar.Visible = True
' Create a button with text on the bar and set some properties.
Set ComBarContrl = ComBar.Controls.Add(Type:=msoControlButton)
With ComBarContrl
.Caption = "Macro1"
.Style = msoButtonCaption
.TooltipText = "Run Macro1"
'the onaction line tells the button to run a certain macro
.OnAction = "Macro1"
End With
' Create a button with an image on the bar and set some
' properties.
Set ComBarContrl = ComBar.Controls.Add(Type:=msoControlButton)
With ComBarContrl
'the facId line will let you choose an icon
' If you choose to use the faceId then the caption is not displayed
.FaceId = 1000
.Caption = "Icon Button"
.TooltipText = "Run Macro2"
'the onaction line tells the button to run a certain macro
.OnAction = "Macro2"
End With
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & vbCr & Err.Description
Exit Sub
End Sub
Sub Macro1()
MsgBox "You have clicked a button to run Macro1"
End Sub
Sub Macro2()
MsgBox "You have clicked a button to run Macro2"
End Sub
'Code to delete the toolbar called by workbook close:
Sub DeleteToolbar()
On Error Resume Next
CommandBars("My Toolbar").Delete
End Sub
|