add a button in the standard toolbal

K

kanwal

Hello,

I am trying to add a button in the standard toolbar and load it using
startup folder templates.

I have found this code - which is adding a button (Version number) but i
dont understand how can i add my custom button in it ?

Also, i want the code to check if the button already exist - if yes, dont
create again.

CustomizationContext = NormalTemplate
CommandBars("Standard").Controls.Add Type:=msoControlButton, ID:=2522,
Before:=4

Can anyone help ?

Thanks

Kanwal
 
L

Lene Fredborg

Hopefully, use can use the code below as the starting point.

In VBA, a button is a CommandBarControl that can have different styles
(msoButtonStyle). The style determines how the button appears in the user
interface. For example, “msoButtonCaption†results in a button that has no
icon but only a written name. If you want to use an icon, you can copy the
icon from another control using CopyFace and apply it to your control using
PasteFace.

I have added some comments in the code that may help you. You will find
information on most of the propteries in the Help. If you want an underlined
character (keyboard accelerator) in the button name so that the command can
be executed by pressing Alt+[underlined character], change the Caption - add
an “&†in front of the letter to be underlined (e.g. “&MyControlâ€).

Dim oBar As CommandBar
Dim oControl As CommandBarControl
Dim bControlExists As Boolean
Dim strMyControl As String

'Define name of control - replace by the name you want
strMyControl = "MyControl"

'Define which template to change - may not be Normal.dot
CustomizationContext = NormalTemplate

Set oBar = CommandBars("Standard")
'Check whether control exists
For Each oControl In oBar.Controls
If oControl.Caption = strMyControl Then
bControlExists = True
Exit For
End If
Next oControl

'If control was not found, add it
If bControlExists = False Then
'In the next code line, change Before to the desired position
'If left out, the control will be added at the end of the toolbar
Set oControl = oBar.Controls.Add(Type:=msoControlButton, Before:=1)
'Apply desired caption, functionality, etc.
With oControl
'Define the style of the button - member of msoButtonStyle
'See the Object Browser (F2) for possible values
.Style = msoButtonCaption
.Caption = strMyControl
'Replace the value below by a value that identifies
'the macro that your button is to execute
.OnAction = "MyModule.MyMacro"
'Other possible properties:
'Tag, Parameter, DescriptionText, ShortcutText, TooltipText
End With
End If

Set oBar = Nothing
Set oControl = Nothing

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top