PDA

View Full Version : Want to create a button at menubar



vinay12321
06-25-2009, 05:25 AM
Hi,

I want to create a button at toobar using VBA macro, can anybody provide me the code?

mikerickson
06-25-2009, 06:22 AM
Record a macro of your using Tool>Customize>Toolbars to add a button to the toolbar.

jammer6_9
06-27-2009, 05:58 AM
Hi,

I want to create a button at toobar using VBA macro, can anybody provide me the code?

Hi, this is to start.


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:= _
msoBarTypeMenuBar, 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 DeleteToolbar()
On Error Resume Next
CommandBars("My Toolbar").Delete
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