Macro code to retrieve Dropdown field value within Word?

D

DanaF

I am new at creating Macros and VB code. I am trying to setup a Macro that
is ran upon exit from a dropdown field in Word. I am wanting to check the
value from the dropdown to fill data into form text fields. I have tried
ActiveDocument but can't see what needs to be coded to get the value.
 
H

Helmut Weber

Hi,

like this:

With ActiveDocument.FormFields.Item("Dropdown1")
MsgBox .Result
End With

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jay Freedman

I am new at creating Macros and VB code. I am trying to setup a Macro that
is ran upon exit from a dropdown field in Word. I am wanting to check the
value from the dropdown to fill data into form text fields. I have tried
ActiveDocument but can't see what needs to be coded to get the value.

Hi Dana,

This is an *extremely* simplified example. I'll assume the form has
just one dropdown field and one text form field, with the default
names Dropdown1 and Text1. (You should rename the fields according to
their function, and change the names accordingly in the macro code.)
The dropdown contains the numbers from 1 to 4, and we're going to fill
the text field with the spelled-out numbers.

This exit macro will do:

Sub ExitDropdown1()
Dim myString As String

Select Case _
ActiveDocument.FormFields("Dropdown1").DropDown.Value
Case 1
myString = "one"
Case 2
myString = "two"
Case 3
myString = "three"
Case 4
myString = "four"
Case Else
' should never get here
myString = "invalid value"
End Select
ActiveDocument.FormFields("Text1").Result = myString
End Sub

You can see from this how to address the formfields by their names as
members of the FormFields collection.

You can use the .Dropdown property only if the formfield really is a
dropdown; otherwise you'll get a runtime error.

The Select Case construction is like having a series of
If...Then...Else expressions. Exactly one case will match any value
from the dropdown, and only that case will execute.

The last statement shows how to put the string value into the text
form field.

In a real form, you can put values into more than one text form field,
and you can do much more complicated manipulations of the data. For
instance, the dropdown value can be used to look up data in a table or
a database, or get values from AutoText entries.
 

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