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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Fields in Word 1

Status
Not open for further replies.

spruceni

Technical User
May 18, 2007
72
GB
I have been given a Word Document which contains a number of form fields. When you look in the properties each has been given a bookmark. In the past I have used routines which write text to bookmarks but in this case the reply is there are no bookmarks to write to. I have not written directly to formfields before. A test routine of my old style is given below.

How do I access the formfields and their bookmark and how do I write to them?

They are not all text fields. One is a checkbox formfield. How do I set this field?

With wordobj.Selection

.Goto what:=wdGoToBookmark, Name:="txtName"
.TypeText ("Test Name")
.Goto what:=wdGoToBookmark, Name:="txtAddline1"
.TypeText ("First line address")
.Goto what:=wdGoToBookmark, Name:="txtAddline4"
.TypeText ("Address line four")
End With


Thanks for any help.
 
Straight from the VBA help about the Formfield object:
ActiveDocument.FormFields("Text1").Result = "Don Funk"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 


Thank you for the reply
I can now write to the formfields. However I can not get the check box form field to be set using the statement below. I also can not find the formfield entry in the VB help. When I type in formfield it returns no entries.


ActiveDocument.FormFields("CheckReading").Result = True
 
Thanks for the pointer to the correct routine all solved now



 
Hi spruceni,

You can also update the formfield via its bookmark:
Code:
ActiveDocument.Bookmarks("BookmarkName").Range.Fields(1).Result.Text = "My String"
where 'BookmarkName' is your bookmark's name and 'My String' is the desired string. In a more generalised sense you could code along the lines of:
Code:
Sub UpdateFormField(FFName As String, FFText As String)
ActiveDocument.Bookmarks(FFName).Range.Fields(1).Result.Text = FFText
End Sub
where 'FFName' is a variable containing your bookmark's name and 'FFText' is a variable containing the desired string.

The advantage of this approach over the one posted by PHV is that it allows you to use strings longer than 255 characters.

Cheers

[MS MVP - Word]
 
Thankyou for your suggestion I will use that routine to cut down the code when writing to form. I also found out from the internet that the call to write to a formfield check box looks like

ActiveDocument.FormFields("Afraid of outdoors").checkbox.value = True

Thanks

 
Hi,

I hope that it's not too late to add a 'rider' to this thread.

I am looking to do pretty much the exact same thing and this thread was very helpful but I have run into one problem: I want write out a value dynamically to the 'active' field, ie not use the bookmark value.

I have many comboboxes on my document and they all function the same. They need to display more than 25 entries so I followed an example I found and created a dialog box that pops up when entering the field. The dialog has the combo on it and then the user makes their selection, clicks 'Close' and the field gets updated. The problem is I have 20 fields that all need to work the same. So I want to reuse the same code and assign the selection to the 'active' field. I looked through the properties and methods but nothing jumped out at me. Is there a way to find the active form on a document?

Thanks
Shane
 
Shane,

Please post your question in a NEW THREAD.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top