How to use VBA to add a macro to a toolbar in Word

F

Frank

I want to use VBA to add a macro to a toolbar in Word.

When I record a macro doing this manually, it just leaves this VBA-command:

CommandBars("Menu Bar").Controls.Add Type:=msoControlButton, Before:= 19

without any reference to the name of the selected macro.

How do I write the VBA-code to add a given macro (say Normal.Modul1.Mymacro)
to the Standard toolbar (or a custom one) and editing the name property to
Mymacro?

Thanks, Frank
 
J

Jonathan West

Hi Frank,



Frank said:
I want to use VBA to add a macro to a toolbar in Word.

When I record a macro doing this manually, it just leaves this
VBA-command:

CommandBars("Menu Bar").Controls.Add Type:=msoControlButton, Before:= 19

without any reference to the name of the selected macro.

How do I write the VBA-code to add a given macro (say
Normal.Modul1.Mymacro)
to the Standard toolbar (or a custom one) and editing the name property to
Mymacro?

After adding it, you set the OnAction property of the button to the name of
the macro, like this

Dim oButton as CommandBarControl
Set oButton = CommandBars("Menu Bar").Controls.Add( _
Type:=msoControlButton, Before:= 19)
oButton.OnAction = "Modul1.Mymacro"
 
F

Frank

Thanks for the solution.

However, I could not find any usable "text only" property on the command bar
control to make it visible.

How is this done in VBA?


Frank
 
J

Jonathan West

Frank said:
Thanks for the solution.

However, I could not find any usable "text only" property on the command
bar
control to make it visible.

How is this done in VBA?

You set the Caption property to determine the caption text, and you set the
Style property to msoButtonCaption in order to have the button display as a
text button.

Dim oButton as CommandBarControl
Set oButton = CommandBars("Menu Bar").Controls.Add( _
Type:=msoControlButton, Before:= 19)
oButton.OnAction = "Modul1.Mymacro"
oButton.Caption = "My macro"
oButton.Style = msoButtonCaption
 

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