Iterating Through Checkboxes

J

Jack

Hello,

I have a Word doc with many textboxes and checkboxes that are part of the
fields collection. I'm having trouble with getting the value from the
checkboxes. The result property doesn't contain the value and I've tried
other ways of getting it, like referencing the associated formfield object
using the bookmarkid on the checkbox field. Two types of sample code is
below. Niether is correct because when iterating through the fields
collection, both lines of code will reference text boxes. I've tested this by
using the .select method so I could see which objects are being selected. If
it helps, the text boxes and check boxes are set inside Word tables.

------------------------------------------------------------------------------
Dim oField As Field
For Each oField In Me.Fields
If oField.Type = wdFieldFormCheckBox Then
If oField.Code.BookmarkID > 0 Then
' SAMPLE 1
MsgBox Me.FormFields(oField.Code.BookmarkID).CheckBox.Value

' SAMPLE 2
MsgBox
Me.FormFields(Me.Bookmarks(oField.Code.BookmarkID).Name).CheckBox.Value
End If
End If
Next

---------------------------------------------------------------------------------

Any help with what I'm doing wrong and the proper way of getting the
checkbox True or False value would be great and much appreciated.

Thanks
Jack
 
J

Jean-Guy Marcil

Jack was telling us:
Jack nous racontait que :
Hello,

I have a Word doc with many textboxes and checkboxes that are part of
the fields collection. I'm having trouble with getting the value from
the checkboxes. The result property doesn't contain the value and
I've tried other ways of getting it, like referencing the associated
formfield object using the bookmarkid on the checkbox field. Two
types of sample code is below. Niether is correct because when
iterating through the fields collection, both lines of code will
reference text boxes. I've tested this by using the .select method so
I could see which objects are being selected. If it helps, the text
boxes and check boxes are set inside Word tables.

SAMPLE 1 as posted works perfectly well on my machine... (Word 2003)
Makes sure your formfields were not copied/pasted in the document, if they
were, make sure each has a unique bookmark name and ID.
It is usually better to create each formfield from scratch when you are
going to manipulate them in this way.
------------------------------------------------------------------------------
Dim oField As Field
For Each oField In Me.Fields
If oField.Type = wdFieldFormCheckBox Then
If oField.Code.BookmarkID > 0 Then
' SAMPLE 1
MsgBox
Me.FormFields(oField.Code.BookmarkID).CheckBox.Value

' SAMPLE 2
MsgBox
Me.FormFields(Me.Bookmarks(oField.Code.BookmarkID).Name).CheckBox.Value
End If
End If
Next

---------------------------------------------------------------------------------

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jack

Merci beaucoup (that's almost the extent of my two years of high school
francais) for your response. I appreciate it.

So there is no other way to get the value of the checkbox from the "field"
object? The only way is to get the value is from the formfield object using
the bookmarkid?

Jack
 
J

Jean-Guy Marcil

Jack was telling us:
Jack nous racontait que :
Merci beaucoup (that's almost the extent of my two years of high
school francais) for your response. I appreciate it.

So there is no other way to get the value of the checkbox from the
"field" object? The only way is to get the value is from the
formfield object using the bookmarkid?

I am not sure why you are having problems with the code you posted... Have
you considered what I mentioned regarding the copying/pasting of formfields
when creating the template?

Otherwise, you can use the index value:

'_______________________________________
Dim oField As Field
Dim MyDoc As Document

Set MyDoc = ActiveDocument
With MyDoc
For Each oField In .Fields
If oField.Type = wdFieldFormCheckBox Then
MsgBox .FormFields(oField.Index).CheckBox.Value
End If
Next
End With
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jack

It's really weird. The oField.Type indicates that the object is a checkbox,
but when I use the line ".FormFields(Field.Index).CheckBox.Value" I get a
"bad parameter" error because the object is actually a text box or expression
box or something else.

Here is the exact code block I have which is modified to accommodate the way
formed it:

Dim oField As Field
Dim MyDoc As Document

Set MyDoc = ActiveDocument
With MyDoc
For Each oField In .Fields
If oField.Type = wdFieldFormCheckBox Then
MsgBox .FormFields(oField.Index).CheckBox.Value & "," &
..FormFields(oField.Index).Name
End If
Next
End With

I'm very confused.

jack
 
J

Jack

I think I know what the problem is. The number of formfields in the
formfields collection does not match the number of fields in the fields
collection. I think because of that, the index for a given field object
doesn't match the index of a given formfield object. So, I think what I'll
need to do is process the non-checkbox values using the fields collection and
make a second pass through the formfields collection for all the checkboxes
(I know they are all accounted for there). I don't think there is any other
way around this. This wouldn't be an issue if the field object held the value
of the check box.

Jack
 
J

Jean-Guy Marcil

Jack was telling us:
Jack nous racontait que :
I think I know what the problem is. The number of formfields in the
formfields collection does not match the number of fields in the
fields collection. I think because of that, the index for a given
field object doesn't match the index of a given formfield object. So,
I think what I'll need to do is process the non-checkbox values using
the fields collection and make a second pass through the formfields
collection for all the checkboxes (I know they are all accounted for
there). I don't think there is any other way around this. This
wouldn't be an issue if the field object held the value of the check
box.

If you have a mix of fields and formfields then only iterate the formfield
collection:

'_______________________________________
Dim oField As FormField
Dim MyDoc As Document

Set MyDoc = ActiveDocument
With MyDoc
For Each oField In .FormFields
If oField.Type = wdFieldFormCheckBox Then
MsgBox oField.CheckBox.Value
End If
Next
End With

'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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