Control Toolbox Controls in Documents

G

Greg Maxey

I am stumgling in the dark trying learn something to help a poster.

I have a Word document with some controls entered in the document using
the Control Toolbox. Specifically I have some options buttons named
Pass1 through Pass4.
I can run Sub Test1() to return the caption of Pass1. I can further
specifically (and I don't know if it is right or not) declare the
variable as a MSForms.OptionsButton and get the result using Sub
Test2(). What I can't figure out is how to cycle through each control
in the document and determine the caption.

It seems that there is no:

For each Ct in Me.Controls or Me.MSForms.OptionButtons or Me.Anything
that will work.

Can anyone help or confirm the quest is hopeless. Thanks.

Sub Test1()
Dim Ct
Set Ct = Me.Pass1
MsgBox Ct.Caption
End Sub

Sub Test2()
Dim Ct As MSForms.OptionButton
Set Ct = Me.Pass1
MsgBox Ct.Caption
End Sub

Sub Test3()
Dim Ct As MSForms.OptionButton
For Each Ct In Me (what would go here to get name of each OptionButton
in the form).
MsgBox Ct.Caption
Next
End Sub
 
J

Jay Freedman

Hi Greg,

It certainly wasn't easy to find this out -- it took some spelunking
in the Object Browser for likely bits of terms. :-b

The Control Toolbox controls are members of either the InlineShapes
collection or the Shapes collection, depending on whether they're
inline or floating, just like pictures.

For inline controls, use a loop like this:

Sub foo1()
Dim ctl As InlineShape
For Each ctl In ActiveDocument.InlineShapes
If ctl.Type = wdInlineShapeOLEControlObject Then
MsgBox ctl.OLEFormat.Object.Caption
End If
Next
End Sub

For floating controls, use a loop like this:

Sub foo2()
Dim ctl As Shape
For Each ctl In ActiveDocument.Shapes
If ctl.Type = msoOLEControlObject Then
MsgBox ctl.OLEFormat.Object.Caption
End If
Next
End Sub

If it's possible that both kinds are present, you have a heck of a
mess. I guess you could loop through the Paragraphs collection looking
for first one kind and then the other...

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Greg Maxey

Jay,

Wow. Good detective work. I will play around with this some. Thanks. I
wonder why:
Sub Test2()
Dim Ct As MSForms.OptionButton
Set Ct = Me.Pass1
MsgBox Ct.Caption
End Sub

Didn't throw some kind of error?
 
J

Jay Freedman

Jay,

Wow. Good detective work. I will play around with this some. Thanks. I
wonder why:


Didn't throw some kind of error?

The control *is* an MSForms.OptionButton, so your code works
correctly. There's no error to throw. But, as you noted originally,
there is no collection whose members are OptionButtons, so you're
limited to working on one button and you need to know its name in
advance.

The Word object model (both the code of Word itself and the
underpinnings of VBA that represent it) pulls together a huge pile of
different kinds of objects into two collections, InlineShapes and
Shapes.

An individual member of either collection can come from any one of an
assortment of fundamental object types. For example, a Shape could
"really" be a picture, an AutoShape (or a set of grouped AutoShapes),
a text box, an embedded object from Excel or PowerPoint or some other
OLE server, one of these ActiveX controls, or just about anything else
that can be inserted in a document as a floating object. The .Type
property tells you which kind of object the Shape "really is".

My code says "if this thing is an OLE control object, then it has an
..OLEFormat property that represents its real nature, and inside that
is the actual .Object, and that has a .Caption property." It's the
buried .Object that "really is" an OptionButton.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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