PDA

View Full Version : Solved: Using varable as formfield name



MrHanky
11-14-2008, 07:27 AM
PLEASE IGNORE, I have now figured out a different way.












Hello,

Im just wondering if someone could shed some light on my poor coding below? I am trying to cycle through the checkboxes and evaluate them one by one. The use of the msgbox below is just there to see if it worked, and once it is working I will substitute that part of the code for something whch then goes and references Excel (That part is ok at the minute).

Could someone point out what I have done wrong here? This is one part of the puzzle and I have tried it a few different ways so far, with no success.


Public Function Role_Check()
For Each ffield In ActiveDocument.FormFields
fName = ffield.Name

If ffield(fName).CheckBox.Value = True Then
MsgBox "Checkbox x is checked", vbOKOnly
Else
MsgBox "Checkbox x is not checked", vbOKOnly

End If
Next
End Function

Thanks for any help

Mark

fumei
11-14-2008, 10:31 AM
Then could you please post what is your different way? Someone else may be helped by it.

Thanks.

MrHanky
11-15-2008, 01:18 AM
Then could you please post what is your different way? Someone else may be helped by it.

Thanks.

Oh sorry, I will post it on Monday when I get back to work, as I cannot remember it off hand.

Thanks

Mark

MrHanky
11-17-2008, 10:02 AM
Here is what I am using now. This bt is just to retrive results from dropdown boxes. Use same logic for checkboxes. As an example I have just sent result to a Msgbox, but once I am finshed I will hopefully be passing the result into an Array so that I can search Excel for it and then retrieve any matching results.

Sub GetDrp()

'get values of drop downs
Dim ofldDRP As FormField
For Each ofldDRP In ActiveDocument.FormFields
If ofldDRP.Type = wdFieldFormDropDown Then
MsgBox ofldDRP.Result


End If
Next

End Sub

fumei
11-17-2008, 10:41 AM
Or, going back to your OP...the demo attached displays a message with the results of both checkboxes and dropdown selections. Do something with the checkboxes and the dropdowns and then click "Check Results" on the top toolbar.Option Explicit

Sub yadda()
Dim oFF As FormField
Dim CheckMsg As String
Dim DropdownMsg As String
For Each oFF In ActiveDocument.FormFields
Select Case oFF.Type
Case wdFieldFormCheckBox
If oFF.Result = True Then
CheckMsg = CheckMsg & oFF.Name & " is checked." & _
vbCrLf
Else
CheckMsg = CheckMsg & oFF.Name & " is NOT checked." & _
vbCrLf
End If
Case wdFieldFormDropDown
DropdownMsg = DropdownMsg & oFF.Name & " value is " & _
oFF.Result & vbCrLf
End Select
Next
MsgBox CheckMsg & vbCrLf & DropdownMsg
End Sub



Note that in your code:
For Each ffield In ActiveDocument.FormFields
fName = ffield.Name

If ffield(fName).CheckBox.Value = True Then

it really should be:

For Each ffield In ActiveDocument.FormFields
If ffield.Value = True Then

in fact,
ffield(fName).CheckBox.Value

should give you an error.

MrHanky
11-19-2008, 05:45 AM
Thanks very much for your help Fumei. That works great.

Mark