Controls Collections for Active Document

W

Weste

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
 
J

Jonathan West

Weste said:
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
 
H

Helmut Weber

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"
 
Top