Programming toolbar buttons

J

JB

Hi Folks,

I'm using the code below to create toolbars based on toolbar settings in
another template (for my own reasons) which are stored in an ini file.

Everything works great (toolbars are created with all the appropriate
details) except they do not execute my required styles. The buttons
which have macros assigned to them work fine but the buttons I used for
style shortcuts do not. I assumed that the name of the style assigned
to a macro being the same as a style would be enough but it seems I'm a
muppet.

Any pointers??

Cheers

J

CustomizationContext = ActiveDocument

i = 0
For x = 0 To UBound(strMainCBInfo()) - 1

Set oCMdBar = CommandBars.Add(Name:=strMainCBInfo(i, 2),
Position:=msoBarFloating, Temporary:=False)
j = 1

With oCMdBar
.Height = strMainCBInfo(i, 0)
.Left = strMainCBInfo(i, 1)
.Name = strMainCBInfo(i, 2)
.NameLocal = strMainCBInfo(i, 3)
.Position = strMainCBInfo(i, 4)
.RowIndex = strMainCBInfo(i, 5)
End With
y = 1

For j = 0 To UBound(strControlInfo()) - 1
If strControlInfo(j, 1, i) <> "" Then

With oCMdBar
.Controls.Add Type:=msoControlButton
With .Controls.Item(y)
.BeginGroup = strControlInfo(j, 0, i)
.Caption = strControlInfo(j, 1, i)
.DescriptionText = strControlInfo(j, 2, i)
.Enabled = True

.FaceId = strControlInfo(j, 3, i)
.Height = strControlInfo(j, 4, i)
'.ID = strControlInfo(j, 5, i)
.OnAction = strControlInfo(j, 6, i)
.Parameter = strControlInfo(j, 7, i)
.Priority = strControlInfo(j, 8, i)
.ShortcutText = strControlInfo(j, 9, i)
.State = strControlInfo(j, 10, i)
.Style = strControlInfo(j, 11, i)
.Tag = strControlInfo(j, 12, i)
.TooltipText = strControlInfo(j, 13, i)
.Width = strControlInfo(j, 14, i)

End With
.Visible = True
End With
End If
y = y + 1
Next j
i = i + 1
Next x
 
S

Shauna Kelly

Hi JB
I assumed that the name of the style assigned to a macro being the same as
a style would be enough

No, it doesn't work like that. Try something like the following:

Sub AddToolbarWithStyle()

Dim oBar As CommandBar
Dim oButton As CommandBarButton
Dim sStyle As String

Set oBar = Application.CommandBars.Add(Name:="My command bar")

sStyle = "Body Text"
Set oButton = oBar.Controls.Add(Type:=msoControlButton, _
ID:=2321, Parameter:=sStyle)
oButton.Caption = sStyle

End Sub

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
J

JB

Shauna said:
Hi JB




No, it doesn't work like that. Try something like the following:

Sub AddToolbarWithStyle()

Dim oBar As CommandBar
Dim oButton As CommandBarButton
Dim sStyle As String

Set oBar = Application.CommandBars.Add(Name:="My command bar")

sStyle = "Body Text"
Set oButton = oBar.Controls.Add(Type:=msoControlButton, _
ID:=2321, Parameter:=sStyle)
oButton.Caption = sStyle

End Sub

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
Hi Shauna,

This looks to be the same as I'm doing just now but with a different ID.
and setting the param at Add time. Does it matter if it's added at Add
time or not? What's the ID portion of your code doing here?

Cheers

J
 
S

Shauna Kelly

Hi JB

Several things are going on here.

1. The .OnAction property, according to VBA help, "Returns or sets the name
of a Visual Basic procedure...". So, if you set the .OnAction property to
(eg) "Body Text", and click the button, then Word will run around trying to
find a VB procedure called "Body Text". But it obviously can't find one.

2. The ID property of a button is the unique read-only number assignd to
each built-in button in Word. For example, ID 3462 is the button for the
"Insert Page Break" command. If you wanted to add a button to invoke the
Insert Page Break command, you'd set the ID to 3462. All buttons that you
create and to which you assign a macro have an ID of 1. I interpret 1 to be
the "Run my macro" command.

3. If you use the macro recorder to record creating a button for a style,
you'll see that your code puts a built-in button with an ID of 2321 on your
toolbar. ID 2321 is the "Apply Style Name" command, which, as far as I'm
aware, isn't documented anywhere. So the code is telling Word to put a
button on your toolbar that will apply a style.

4. How Word uses the .Parameter property depends on the command being
invoked. In the case of ID 2321, the parameter tells Word which style to
apply. So if you created a button with ID 2321, without providing a
parameter, Word wouldn't know what style to apply, and the button does
nothing.

5. If you don't set the .Caption property, the button will simply say "Apply
Style Name". So you need to set the Caption property for the button to be
useful.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
J

JB

Shauna said:
Hi JB

Several things are going on here.

1. The .OnAction property, according to VBA help, "Returns or sets the name
of a Visual Basic procedure...". So, if you set the .OnAction property to
(eg) "Body Text", and click the button, then Word will run around trying to
find a VB procedure called "Body Text". But it obviously can't find one.

2. The ID property of a button is the unique read-only number assignd to
each built-in button in Word. For example, ID 3462 is the button for the
"Insert Page Break" command. If you wanted to add a button to invoke the
Insert Page Break command, you'd set the ID to 3462. All buttons that you
create and to which you assign a macro have an ID of 1. I interpret 1 to be
the "Run my macro" command.

3. If you use the macro recorder to record creating a button for a style,
you'll see that your code puts a built-in button with an ID of 2321 on your
toolbar. ID 2321 is the "Apply Style Name" command, which, as far as I'm
aware, isn't documented anywhere. So the code is telling Word to put a
button on your toolbar that will apply a style.

4. How Word uses the .Parameter property depends on the command being
invoked. In the case of ID 2321, the parameter tells Word which style to
apply. So if you created a button with ID 2321, without providing a
parameter, Word wouldn't know what style to apply, and the button does
nothing.

5. If you don't set the .Caption property, the button will simply say "Apply
Style Name". So you need to set the Caption property for the button to be
useful.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
Thanks very much for your explanation Shauna.

Clears things up for me.

Cheers

J
 

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