PDA

View Full Version : How to loop over checkboxes in word?



GoldServe
04-20-2007, 06:04 PM
I know the me.controls("abc" & i).value trick but this does not exist in word? Is there a way to loop over controls?

Jacob Hilderbrand
04-20-2007, 08:07 PM
Try something like:


Dim F As FormField

For Each F In ActiveDocument.FormFields
MsgBox F.Name & " (" & F.Type & ")"
Next

GoldServe
04-20-2007, 09:12 PM
Sorry I forgot to say, I am using ActiveX controls that are named Checkbox1, checkbox2, etc...

fumei
04-21-2007, 01:15 AM
ActiveX controls in Word are in the InlineShapes collection. Here is an example that checks if the InlineShape is a checkbox, and if it is messages whether it is checked, or not.Dim oCtl As InlineShape
On Error Resume Next
For Each oCtl In ActiveDocument.InlineShapes
If oCtl.OLEFormat.ProgID = "Forms.CheckBox.1" Then
If oCtl.OLEFormat.Object.Value = True Then
MsgBox "Checkbox " & oCtl.OLEFormat.Object.Name & _
"is checked"
Else
MsgBox "Checkbox " & oCtl.OLEFormat.Object.Name & _
"is NOT checked"
End If
End If
NextYou need the on error instruction in case you have other InlineShapes, like pictures. Otherwise it stops execution, as image InlineShapes have no OLEFormat.ProgID.

You could also try testing using:oCtl.Type=wdInlineShapeOLEControlObject
Hard to say, as I don't know what your loop is trying to do.

fumei
04-21-2007, 01:17 AM
Note that .Name, and .Value do NOT show up with IntelliSense for OLEFormat.Object.

I only added/used .Name as I always explicitly name objects. You apparently do not, as you mention Checkbox1, Checkbox2 etc.