Getting a dropdown value from the fields collection

J

Jack

Hello,

I have an application that is looping through the fields collection of a
Word document and gathering the entered values and subsequently loading those
values into an Access database.

Although I can get the value of the dropdown box from the Result property
when accessing the object using the formfields collection, I cannot seem to
get the value using the fields collection. To prevent significant coding
changes to my routine, can anyone advise me on how to get the value selected
in a dropdown formfield from the fields collection?

Thanks in advance!
Jack
 
J

Jay Freedman

Hello,

I have an application that is looping through the fields collection of a
Word document and gathering the entered values and subsequently loading those
values into an Access database.

Although I can get the value of the dropdown box from the Result property
when accessing the object using the formfields collection, I cannot seem to
get the value using the fields collection. To prevent significant coding
changes to my routine, can anyone advise me on how to get the value selected
in a dropdown formfield from the fields collection?

Thanks in advance!
Jack

Hi Jack,

As the saying goes, you ain't gonna believe this one! As you saw
already, the Fields collection and the FormFields collection aren't
connected (how stupid is that?). Here's what you have to do to get
from here to there:

Sub foo()
Dim oFld As Field
Dim oRg As Range
Dim oFfld As FormField
Dim numrslt As Long
Dim rslt As String

For Each oFld In ActiveDocument.Fields
With oFld
If .Type = wdFieldFormDropDown Then
Set oRg = .Result ' range of interior of dropdown
' expand the range by 1 char left & right
oRg.MoveStart unit:=wdCharacter, Count:=-1
oRg.MoveEnd unit:=wdCharacter, Count:=1
' grab the FormField from the range
Set oFfld = oRg.FormFields(1)
' get the index of the selected value
numrslt = oFfld.DropDown.Value
' get the text of the selected value
rslt = oFfld.DropDown.ListEntries(numrslt).Name

' do what you like with it
MsgBox rslt
End If
End With
Next oFld
End Sub
 
J

Jack

Works perfectly. Thanks so much!


Jay Freedman said:
Hi Jack,

As the saying goes, you ain't gonna believe this one! As you saw
already, the Fields collection and the FormFields collection aren't
connected (how stupid is that?). Here's what you have to do to get
from here to there:

Sub foo()
Dim oFld As Field
Dim oRg As Range
Dim oFfld As FormField
Dim numrslt As Long
Dim rslt As String

For Each oFld In ActiveDocument.Fields
With oFld
If .Type = wdFieldFormDropDown Then
Set oRg = .Result ' range of interior of dropdown
' expand the range by 1 char left & right
oRg.MoveStart unit:=wdCharacter, Count:=-1
oRg.MoveEnd unit:=wdCharacter, Count:=1
' grab the FormField from the range
Set oFfld = oRg.FormFields(1)
' get the index of the selected value
numrslt = oFfld.DropDown.Value
' get the text of the selected value
rslt = oFfld.DropDown.ListEntries(numrslt).Name

' do what you like with it
MsgBox rslt
End If
End With
Next oFld
End Sub
 

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