PDA

View Full Version : Help with looping through Text Form Fields please...



Tecnik
07-09-2007, 10:23 AM
Hi there,

Please can someone help me with this query.

I have a word doc that has quite a few Text Form Fields in it. What I'm trying to do is write a macro that will execute the following code on fields that have a certain label ('bookmark' in the properties dialog) prefix.

For example the word doc may have 6 fields named:-

dog1
dog2
cat1
cat2
cat3
cat4

On running the macro it would loop through all the fields that have the prefix 'cat' and leave all the ones prefixed with 'dog'.

This is the code I'm wanting to apply to each one:-

Sub check()
With Selection.FormFields(1)
.Enabled = True
End With
End Sub

At present I've got some buttons set up to check or uncheck the 'Fill-in enabled' check box, I'd like to do this in one go if possible.
Can I create an object reference to all the fields that have a certain label prefix?
I've found a few looping examples but I'm not sure if I can just create reference to all the fields I need, or will I have to loop through all the fields and check the prefix on each one?

Thanks in advance,

Nick

fumei
07-09-2007, 11:07 AM
See attached demo. There are three macros that can be fired. They are on the top toolbar: Disable Dogs, Enable All, ShowCats.

1. You can disable directly using a string.Sub DogDisable()
Dim docFF As FormFields
Dim oFF As FormField
Set docFF = ActiveDocument.FormFields
For Each oFF In docFF
If Left(oFF.Name, 3) <> "cat" Then
oFF.Enabled = False
End If
Next
Set docFF = Nothing
End SubDisables all formfields with names NOT starting with "cat".

Can you make an object reference of just the ones with "cat"? Yes. You make an array of formfield objects. You filter the formfield collection for the ones starting with "cat". If one does start with "cat", you add it to the array. Like this:Dim docFF As FormFields
Dim oFF As FormField
Dim FF_Array() As FormField
Dim j As Long
Set docFF = ActiveDocument.FormFields
For Each oFF In docFF
If Left(oFF.Name, 3) = "cat" Then
ReDim Preserve FF_Array(j)
Set FF_Array(j) = oFF
j = j + 1
End If
Next
You can use an item in the array of "cat" formfields as a formfield object...which they are.Msgbox FF_Array(0).Resultwould display the text from the first "cat" formfield found. Remember, while your formfields may be "cat1", "cat2" etc., the array is 0-based.

Or, say you want to check if any of the "cat" formfields says "angora". Once you build the array of "cat" formfields (FF_Array)....Dim var
For var = 0 To Ubound(FF_Array)
If FF_Array(var).Result = "angora" Then
' whatever you want to do

The macro ShowCats (on the toolbar) demonstrates this. There is some error trapping.

1. I hardcoded the fact the document has three formfields with "cat". So the only valid input to request to see the result (the content) is 1, 2, or 3. There are error messages to respond to invalid input.

2. there are error messages if the formfield is blank.

So......

Click Enable All to make all the formfields enabled.

Click Disable Dogs to disable all non-"cat" named formfields.

Click ShowCats to see the result of a specifed "cat" formfield. Put something IN a cat formfield first - otherwise you get error messages.

Tecnik
07-10-2007, 02:40 AM
Hi Gerry,

Thank you for your reply, the code and the demo doc which does exactly what I want.
I found the information very informative, it will come in useful. It was good to see the different variations.

Thanks once again,

Regards,

Nick

mdmackillop
07-10-2007, 01:35 PM
Hi Nick,
Don't forget to mark your posts Solved.
Regards
MD