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!

How to populate Word Fields via VBA Code?

Status
Not open for further replies.

jsteph

Technical User
Oct 24, 2002
2,562
US
I'm trying to do something that I think is fairly simple: I've got a document, with several fields. I want to--via code from either Access application or VB application--automatically enter text into each of the fields. Some of the fields are dropdowns, some just text.

Now, I got thrown off track when I started looking at the Fields colletion--it seems each field is also a Bookmark? Anyway, I just want to loop through the Fields Collection (Or Bookmarks or whatever these are??) and enter text in each one based on data I get from a database.

I'm fine with the code for database stuff and opening the word document object and all of that, but when I get to the Field (or is it Bookmark) object, I don't see any method that has anything to do with assigning a value, and no Property like ".Value" or ".Text". There is a .Data, but I get an error saying "Field can't contain Data" when I access this property.

Thanks for any help on this,
--Jim
 
Use the .Result property.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,
Thanks, that works for text type fields, but not for the dropdowns. For them, .Result returns blank and setting .Result appends the text to the end of what was there. Do you know how to choose an ordinal of the choices in the dropdown, or just set the value regardless of choices?

Chance,
Thanks for the faq, it helps for bookmarks. I'm looking through Word vba references and can't find much about the dropdowns.
--Jim
 
Never mind, got it, it's

Doc.FormFields(i).Dropdown.Value
--Jim
 
Aside from the drop down issue:
If you populate your document with one or more bookmarks named *exactly* as your fields in the recordset, you could try this:

Sub WdInsertData()
For Each bkm In WdDocument.Bookmarks
With Selection
For Each fld In rst.Fields
If fld.NAME = bkm.NAME Then
If IsNull(fld.Value) Then
bkm.Range.InsertAfter Text:=""
Else
bkm.Range.InsertAfter Text:=fld.Value
End If
End If
Next fld
End With
Next bkm
End Sub

With this code it doesn't matter where, or how many bookmarks there are or in what order. I have a range of doc templates with different styles, each needing field results in different places to suit. The above works a treat.
 
If you are adding entries to the dropdown, the syntax is:

Code:
With Doc.Formfields(i).DropDown.ListEntries
    .Add Name:="Red"
    .Add Name:="Blue"
    .Add Name:="Green"
End With

Also note that .Value does NOT return the value of the item, it returns the value if the INDEX.

From the above,
Code:
Doc.Formfields(i).Dropdown.Value
returns 1 - assuming no one has changed the selection. If someone had selected "Green" from the dropdown, then .Dropdown.Value returns 3, NOT "Green".

To get the text of the item you need to do:

Code:
Doc.FormFields(i).DropDown.ListEntries _
  .Item(Doc.FormFields(i)DropDown.Value).Name

This uses the .Value to get the index number, then uses THAT to get the .Name - which is the text of the item.


Gerry
 
Gerry,
Thanks much, that's good to know,
--Jim
 
Here is where I can make a plug for the Object Browser. By putting an object into Search of the browser you can see the data type for it.

Dropdown.Value is Long - a number
Dropdown.Item(x).Name is String - text

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top