Word VBA for drop down field - enter first letter to see a selection that begins with that letter

Hello ,

I have a Drop Down form field with a list of cities.

Is it possible with VBA code to automate the drop down field so that when a user Tabs into the field, if they type the first letter of the city they desire, the list will display the city that begins with that letter?  (Then ideally, with the desired city chosen, the user can Tab out of the field to select the displayed city.)

Thank you

Mark

Answer
Answer

Or, as macros are permissible in your document, you could create a userform to hold the names of the cities in a list box and display that listbox on entry to the field. List boxes can be configured to select by letter (or the full value).

Assuming a legacy form field create a simple userform in the document e.g.

The code behind the form would be as simple as

Option Explicit

Private Sub CommandButton1_Click()
ActiveDocument.FormFields("Text2").Result = Me.ListBox1.Text
Unload Me
End Sub

Private Sub UserForm_Initialize()
    Me.Caption = "Select City"
    With Me.ListBox1
        .TabIndex = 0
        .AddItem "Albany"
        .AddItem "Birmingham"
        .AddItem "Colchester"
        .AddItem "Doncaster"
        .ListIndex = -1
    End With
End Sub

Include your list of cities in place of those shown and you would call it from a macro, in an ordinary module in the document, run on entry to the form field. I have left the default names, for the elements and have assumed a text form field named 'Text2' You can change the names as appropriate.

Option Explicit

Sub GetCity()
UserForm1.Show
End Sub

When you tab into the field the userform displays and you can make your selection. You are also not limited to the 25 selections in a dropdown form field.

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 1,170 Applies to: