formfield rename

P

Paul

i have a document with numerous (100) formfields in some
have bookmark names some dont.

Can anyone tell me how to loop through each formfield on
the document as i want to automatically rename all of
them without having to manually access each one
individually.

Thanks in anticipation

Paul
 
M

Mark Tangard

Hi Paul,

Looping is easy, coding to rename the fields according to
anything but the simplest pattern is a little tougher.

To cycle through the formfields collection, you must, um,
cycle through the formfields collection: ;) Ordinarily
you'd want to use a For Each loop for that:

Dim ff as FormField
For Each ff in ActiveDocument.FormFields
<---manipulations here
Next ff

BUT: Unless you want to assign them names according to
some aardvark random series, you're more likely to need
something like this:

Dim i As Long
For i = 1 To ActiveDocument.FormFields.Count
ActiveDocument.FormFields(i).Name = "foobar" & i
Next

If you have different sorts of formfields and you want
to note the field type in its name somehow, you might
use:

Dim i As Long
For i = 1 To ActiveDocument.FormFields.Count
With ActiveDocument
If .FormFields(i).Type = wdFieldFormTextInput Then
.FormFields(i).Name = "MyTextField" & i
ElseIf .FormFields(i).Type = wdFieldFormCheckBox Then
.FormFields(i).Name = "MyChex" & i
:
: and so on
:
End If
End With
Next i

As you can see, the more automatic your renaming becomes, the
the less flexible your naming can be. That isn't always the
best situation, but to be sure, with 100 formfields you don't
likely want to give individualized names to each. Dalmatians
they ain't. Post back if this doesn't answer your question
fully.
 
P

Paul

Mark
Thanks for this.

Paul
-----Original Message-----

Hi Paul,

Looping is easy, coding to rename the fields according to
anything but the simplest pattern is a little tougher.

To cycle through the formfields collection, you must, um,
cycle through the formfields collection: ;) Ordinarily
you'd want to use a For Each loop for that:

Dim ff as FormField
For Each ff in ActiveDocument.FormFields
<---manipulations here
Next ff

BUT: Unless you want to assign them names according to
some aardvark random series, you're more likely to need
something like this:

Dim i As Long
For i = 1 To ActiveDocument.FormFields.Count
ActiveDocument.FormFields(i).Name = "foobar" & i
Next

If you have different sorts of formfields and you want
to note the field type in its name somehow, you might
use:

Dim i As Long
For i = 1 To ActiveDocument.FormFields.Count
With ActiveDocument
If .FormFields(i).Type = wdFieldFormTextInput Then
.FormFields(i).Name = "MyTextField" & i
ElseIf .FormFields(i).Type = wdFieldFormCheckBox Then
.FormFields(i).Name = "MyChex" & i
:
: and so on
:
End If
End With
Next i

As you can see, the more automatic your renaming becomes, the
the less flexible your naming can be. That isn't always the
best situation, but to be sure, with 100 formfields you don't
likely want to give individualized names to each. Dalmatians
they ain't. Post back if this doesn't answer your question
fully.

--
Mark Tangard, Microsoft Word MVP
Please reply only to the newsgroup, not by private email.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters




.
 

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