Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Adding / Deleting from toolbars

Steven,

The first two sub below go into the ThisWorkbook object's codemodule, the last two into a regular module. The code will add a
smiley button to the standard toolbar to fire the macro named MacroName (which should be in Module1). The button will be added when
the file is opened, and removed when it is closed.

HTH,
Bernie
Excel MVP

Private Sub Workbook_Open()
AddButton
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
DeleteButton
End Sub

'Code below in standard module Module1
Public myBar As CommandBar
Public myButton As CommandBarButton

Sub AddButton()
DeleteButton
'Create the new button
Set myButton = myBar.Controls.Add(Type:=msoControlButton, ID:=2950)

'Set the properties of the new button
With myButton
.Caption = "Run my Macro"
.OnAction = ThisWorkbook.Name & "!Module1.MacroName"
End With

End Sub

Sub DeleteButton()
Set myBar = Application.CommandBars("Standard")
On Error Resume Next
myBar.Controls("Run my Macro").Delete
End Sub


"Steven Revell" wrote in message ...
Hi,

I was wondering if it was possible to add a command to a
toolbar on workbook open. i need to give it a name in case
someone moves it. I then want to delete it on workbook
close.

Thanks in advance for any help,

Steven



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
Consecutive No'ing WHEN ADDING/DELETING ROWS CMack Excel Discussion (Misc queries) 2 January 17th 08 03:08 PM
Adding macros to toolbars Elsa Excel Discussion (Misc queries) 1 January 30th 07 09:38 PM
Adding and deleting rows with formulas ****Need Help**** [email protected] Excel Discussion (Misc queries) 5 May 31st 06 05:42 PM
Sequential number - adding and deleting rows Suzan Excel Discussion (Misc queries) 2 January 4th 06 09:36 PM
how to protect sheet from adding/deleting Scott Excel Discussion (Misc queries) 1 February 16th 05 05:21 PM


All times are GMT +1. The time now is 03:32 AM.

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

About Us

"It's about Microsoft Excel"