Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word FormField Help

Status
Not open for further replies.

cglinn

Programmer
Sep 3, 2003
44
US
I need to be able to do one of two things, through code:

1. Change the max length of a form field.

or

2. Rename an existing form field.

I've tried changing the name of a form field with the following code. However, it doesn't seem to work, as nothing gets placed in the bookmark field.

With Selection.FormFields(1)
.Name = "Inv" & myRow
.Enabled = False
End With

Thanks in advance.

Chris
 
Have you tried using the macro recorder? It will easily show how to do this.

Code:
With ActiveDocument.FormFields("whatever")
   .Name = "whatever"
   With .TextInput
     .EditType Type:=wdRegularText, Default:="default", _
         Format:=""
     .Width = 25
   End With
End With

1. Do you see any place in your code that shows a max length?

2. The name change should have worked.

3. You wrote:
However, it doesn't seem to work, as nothing gets placed in the bookmark field.
but your questions mention nothing at all about placing anything IN the formfield. You asked about rename and changing the max length. So place WHAT in the formfield??? I am making assumptions that it may be the default text, so indicated how to do that.

However, when posting, please state clearly what you want to happen. I have no real idea what what you mean when you state nothing gets placed in the bookmark field. It seems reasonable to me - you did not place anything there.

Gerry
 
Thank you for responding. What I meant by "Bookmark" was, if you go to the properties of the FormField, the name is stored in a field called "Bookmark". After I set the name, this field is blank when viewed from properties.

Does this make more sense?

 
Yes it does. I have to admit an error myself. I used the SAME name in my sample code!
Code:
With ActiveDocument.FormFields("whatever")
   .Name = "whatever"
   With .TextInput
     .EditType Type:=wdRegularText, Default:="default", _
         Format:=""
     .Width = 25
   End With
End With

should be:
Code:
With ActiveDocument.FormFields("Text1")
   .Name = "whatever"
   With .TextInput
     .EditType Type:=wdRegularText, Default:="default", _
         Format:=""
     .Width = 25  ' this is the MAX length
   End With
End With

When you run the code, the name in the Bookmark Name DOES change.

Version?
Did you try the macro recorder? (Unanswered)
Did you try the code above? Did it work?

Gerry
 
I found the problem. For some reason when the code was copying another formfield it was giving it a blank name. It turns out that if you try to change the name of a formfield, which currently has a blank name, you receive an error. To get around this you can use this code:

Selection.FormFields(1).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "whatever"
.Execute
End With
 
This is why it is better to not copy formfields. The copied one does not have a name. How could it? the original has the name. Names are explicit to their object.

It is better to explicitly create them - if you are doing this by code. Creating them gives the opportunity to name them.

BTW: you have stumbled on one of the strange and annoying aspects of the Word object model.

Select an un-named formfield (a copied one) and turn on the macro recorder, use Shift-F10 to get the equivalent of a right click, get the dialog and name the formfield. On OK, turn off the macro recorder.

The name given in the process persists; the name sticks.

Delete that formfield, and copy a new one - also becomes un-named. Select THAT one, and run the macro. It is will fail.

It is the only case I know of where the macro recorder records steps that work during the recording, but NEVER work otherwise. Recording the steps is a totally useless exercise, as it always fails.

Gerry
 
Again, thank you for your attempt at helping me. You'll have to trust me when I say I have no other choice than to copy existing form fields. However, some of them did retain the original name. Perhaps that is just a bug. Anyway, I have everything working as it should be, using the "with dialogs".

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top