Trying to change the name of a formfield

S

skbrown14

I have code that inserts autotext entries into a document. I need to change
the name of the form fields that are in those autotext entries based on the
number of those entries that are inserted into the document.

For example, I have a form field called DICTATION_N1_x in the autotext
entry. If I insert 2 copies of the autotext entry, I want the first copy to
have the form field named "DICTATION_N1_1" and the second copy to have the
form field named "DICTATION_N1_2".

In doing this programmatically, I am unable to set the .Name property of the
form field to a variable. I am getting an automation error. Does anyone
know how to set the name property on a form field programmatically to a
variable?

Thanks in advance for any help!
 
D

Doug Robbins - Word MVP

ActiveDocument.FormFields(1).Name = "Text3" changes the name of the first
formfield in the document to Text3 for me.

What do you mean by "a variable"? Show us the code that you are trying to
use.
 
S

skbrown14

Thanks and sorry about not elaborating, but here is the code I'm using:

Public Sub ChangeDictBMName(DictAcr As String)
Dim ff As FormField
Dim tempName As String

On Error GoTo ErrHndler

For Each ff In ActiveDocument.FormFields

If Mid$(ff.Name, 11, Str(Len(DictAcr))) = DictAcr And Right$(ff.Name, 1)
= "x" Then
tempName = Left$(ff.Name, Len(ff.Name) - 1) & Str$(glbThisSuppQty)
ff.Name = tempName
End If

Next ff

Exit Sub

ErrHndler:
ret = MsgBox(Err.Number & ": " & Err.Description, , "LC Standard Report:
ChangeDictBMName")

End Sub


Before I enter into this code, I've looked for the autotext entry to see if
it is already inserted into the document by searching for a bookmark, and
determined how many of that entry has already been inserted into the
document. That number is stored in glbThisSuppQty. I then insert the
autotext entry then go into the code above. The passed argument, DictAcr,
holds values such as "N", "PR", "PL", etc. One autotext entry has multiple
form fields named, for example, "DICTATION_N1_x", "DICTATION_N2_x",
"DICTATION_N3_x", etc. The "x" needs to be changed to the number of copies
of that autotext entry.

I hope this helps a little.

Thanks
 
D

Doug Robbins

Are you sure that the duplicate formfields have a name in the first place?
If I insert a formfield Text1, then copy and paste it into another place in
the document, the pasted formfield does not have a name.

See the article "How to assign a Name to a FormField that doesn't already
have a Name, using VBA" at:

http://word.mvps.org/FAQs/MacrosVBA/AssignNameToFmFld.htm


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
S

skbrown14

Thanks, that worked. I actually ran into some problems to begin with but
found out that I was trying to name the formfield with a space in it. Once I
found that and used Trim to take off spaces, the
Dialogs(wdDialogFormFieldOptions).Execute worked great.

Thanks alot!
 

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