Nick
Nick

Reputation: 3337

How to get value of drop down menu in Microsoft Word

I have a Microsft Word document with a drop-down menu in it. I am trying to write a macro that gets the value of the drop down and performs calculations. How do I get the value of the drop-down?

Upvotes: 1

Views: 8244

Answers (1)

Todd
Todd

Reputation: 6179

First, you can get a reference to the dropdown either by the name you gave it (the Bookmark field in the Properties box), or by its number in the sequence of fields you added.

Then you use the Result property to show the currently selected value.

I created a blank document with two dropdown lists on it (show the Forms toolbar, then click a button to insert that control at the cursor position):

ColorDropdown
  red
  green
  blue

SizeDropdown
  small
  medium
  large

then I wrote a few test routines:

Sub ListDropDowns()
  Dim doc As Document, f As FormField
  Set doc = ActiveDocument

  For Each f In doc.FormFields
    Say f.Name & " = " & f.Result
  Next
End Sub

Sub ShowChosenColor()
  Dim f As FormField
  Set f = ActiveDocument.FormFields("ColorDropdown")
  Say "color = " & f.Result
End Sub

Sub Say(s As String)
  Debug.Print s
End Sub

these are the results of ListDropDowns and ShowChosenColor from the Immediate window:

ColorDropdown = blue
SizeDropdown = large

color = blue

some more Immediate window test:

set doc = ActiveDocument

? doc.FormFields(1).Name
ColorDropdown

? doc.FormFields(2).Name
SizeDropdown

? doc.FormFields(2).Result
large


sName = doc.FormFields(2).Name
? sName
SizeDropdown

sSize = doc.FormFields(sName).Result
? sSize
large

Upvotes: 1

Related Questions