I am trying to loop thru all the controls in my Word document - text boxes
and combo boxes. I am not using form fields. Is there a collection I can
use to do this. The controls collection only seems to be available with user
forms and not documents. Thanks for your help!

Weste

Re: Controls Collections for Active Document by Jonathan

Jonathan
Fri Mar 31 06:23:47 CST 2006


"Weste" <Weste@discussions.microsoft.com> wrote in message
news:E1903842-659C-4F6E-98B4-D526D64E2AC1@microsoft.com...
>I am trying to loop thru all the controls in my Word document - text boxes
> and combo boxes. I am not using form fields. Is there a collection I can
> use to do this. The controls collection only seems to be available with
> user
> forms and not documents. Thanks for your help!
>
> Weste

You need to look at the CommandBars collection and the Controls collection
of each Commandbar object.

When doing this, you need to be very careful that you set the
CustomizationContext property correctly. The CustomizationContext defines in
what document, template or add-in the toolbars and buttons are stored. if
you make any change to a button, the change will be stored in the file
specified as the current CustomizationContext.

This is a notoriously tricky part of Word programming. Proceed with caution
and take regular backups.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org


Re: Controls Collections for Active Document by Tony

Tony
Fri Mar 31 06:26:44 CST 2006

Either (or probably both of) the Shapes and the InlineShapes collections.

--
Enjoy,
Tony

"Weste" <Weste@discussions.microsoft.com> wrote in message
news:E1903842-659C-4F6E-98B4-D526D64E2AC1@microsoft.com...
> I am trying to loop thru all the controls in my Word document - text boxes
> and combo boxes. I am not using form fields. Is there a collection I can
> use to do this. The controls collection only seems to be available with
user
> forms and not documents. Thanks for your help!
>
> Weste



Re: Controls Collections for Active Document by Helmut

Helmut
Fri Mar 31 13:29:02 CST 2006

Hi Weste,

to the best of my limited knowledge,
controls form the control toolbox are
either shapes or inlineshapes.

So you might have to loop through all shapes
and inlineshapes and check their type.

Inlineshape controls are of type 5 (= wdInlineShapeOLEControlObject).

Here is an example for inlineshapes, in the doc's mainstory.


Sub test400()
Dim iShp As InlineShape
For Each iShp In ActiveDocument.InlineShapes
If iShp.Type = wdInlineShapeOLEControlObject Then
If iShp.OLEFormat.ClassType = "Forms.TextBox.1" Then
MsgBox "Control Textbox"
End If
If iShp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
MsgBox "Control Combobox"
End If
End If
Next
End Sub

Note, that the comparison is case sensitive,
so "Forms.Textbox.1" won't work,
whereas "Forms.TextBox.1" does.

With shapes, which have to be converted from
inlineshapes to shapes programmatically beforehand,
things seem to be different, as they are
of type msoOLEControlObject,
which means, they are part of Office, not of Word.

Sub test401()
Dim oShp As Shape
For Each oShp In ActiveDocument.Shapes
If oShp.Type = msoOLEControlObject Then
MsgBox oShp.OLEFormat.ClassType
End If
Next
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"