Setting FaceID on Custom Button

D

DerbyDad03

I would like to change, via VBA, either the FaceID and/or the name on
a custom button that I have assigned to a macro.

Specifically, I want to be able click a specific button, have the code
run, and have the name and/or FaceID of *that* button change. It is
essentially a toggle button for an option and I want the FaceID or
name to tell me the current setting of that option.

The button exists and the toggle code runs fine, but I don't know how
to tell VBA which button I want to change.

Can I capture the control number of the button that was pressed via
code or do I have to know it's number beforehand? In either case, how
do I do it?


Thanks!
 
L

Lene Fredborg

Have a look at the VBA help on the ActionControl property. You can use the
property to change the Caption (name) or the FaceID of the control when it is
clicked.

Example:
You want your button to switch between the captions “MyControl On†and
“MyControl Offâ€.
You can obtain this by inserting e.g. the following code in the procedure
that that runs when you click the button:

With CommandBars.ActionControl
If .Caption = "MyControl On" Then
.Caption = "MyControl Off"
Else
.Caption = "MyControl On"
End If
End With

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

DerbyDad03

Thank you for that, it works fine. Now I need to expand the question
a bit.

I would still like to know how to determine the control number (?) for
a specific button. I have a Close event macro that sets the option to
the default that I would prefer, but that could result in the text for
my custom button being opposite of the actual option setting.

Specifically, I'm toggling the "Insert/paste pictures as" between In
Line and In Front. My Close event sets the option to In Front, but
that won't change the button text from "In Line Set" to "In Front Set"
so it matches the option the next time I open Word.

Here's the code I'm using:

Private Sub Document_Close()
'Set Paste Picture option to In Front upon Close
Options.PictureWrapType = wdWrapMergeFront
End Sub

Sub ImagePaste()
'Toggle Paste Picture option and
'Display current setting on button
With CommandBars.ActionControl
If .Caption = "In Line Set" Then
Options.PictureWrapType = wdWrapMergeFront
.Caption = "In Front Set"
Else
Options.PictureWrapType = wdWrapMergeInline
.Caption = "In Line Set"
End If
End With
End Sub

BTW...if this sounds silly to most of you, it's as much as exercise in
learning VBA for Word as well as automating an option change that I
use quite often. I can already think of other places where I can use
the ActionControl property, so I'm glad you pointed it out. I'm fairly
proficient with VBA for Excel and now I'm trying to expand my
horizons.

Thanks again!
 
L

Lene Fredborg

You can, for example, apply a Tag to your control. You can then use that tag
in combination with the FindControl method in order to identify the control.

Example:
You apply the tag “MyTag†to your control (you can set the Tag property in
the same way as you set the caption). If you leave the tag of the control
unchanged and make sure that you use something unique as the tag string, you
can always find the control using the FindControl method (see the VBA help –
you can use more parameters).

Dim oControl As CommandBarControl
Set oControl = CommandBars.FindControl(Tag:="MyTag")

You can now manipulate oControl as needed. For example, you can change the
Caption:
oControl.Caption = "Another Caption"

If I read your description correctly, it should be sufficient to add the
following line of code to your Close event procedure (provided you first
apply the Tag “MyTag†to your control):

CommandBars.FindControl(Tag:="MyTag").Caption = "In Front Set"

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

DerbyDad03

Done...and working. Thanks!

Now, the last question would relate to efficiency.

I added the tag as part of my ActionControl routine, ran it once and
then commented it out, but left it in as a reminder. I didn't see any
other way to add a tag to an existing control, since I don't know how
to find the control via VBA unless it has a tag. <g>

Is that the best way to do it or is there a dropdown someplace that I
don't know about?
 
L

Lene Fredborg

As far as I have experienced, you can only add a tag via VBA. So the method
you used i OK.

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

DerbyDad03

As far as I have experienced, you can only add a tag via VBA. So the method
you used i OK.

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









- Show quoted text -

Cool! Thanks for sticking with me on this one. I learned a bunch -
more than just how to toggle an option.
 

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