Word VBA for drop down field
发布时间:2026-10-13 | 浏览:1
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.)
A family of Microsoft word processing software products for creating web, email, and print documents.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
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
Private Sub UserForm_Initialize()
Me.Caption = "Select City"
With Me.ListBox1
.AddItem "Albany"
.AddItem "Birmingham"
.AddItem "Colchester"
.AddItem "Doncaster"
.ListIndex = -1
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
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.
9 additional answers
Anonymous 2014-12-13T16:31:20+00:00 Hello, I have just now started working in some of these areas of WORD 2010. I am trying to use this solution on a form i am making for work and can't get it to work. Any chance making a for dummies version for me? I will have multiple fields that i would like to use. (this is for a Missing Person entry form) The first field that needs a option list is a race field. W - White B - Black A - Asian I - American Indian U - Unknown I tried copying your code from above into the VBA editor, but not sure where to put it. or how to call it in the form itself under the macro selection. 0 comments No comments
I have just now started working in some of these areas of WORD 2010.
I am trying to use this solution on a form i am making for work and can't get it to work.
Any chance making a for dummies version for me?
I will have multiple fields that i would like to use. (this is for a Missing Person entry form) The first field that needs a option list is a race field.
I - American Indian
I tried copying your code from above into the VBA editor, but not sure where to put it. or how to call it in the form itself under the macro selection.
Anonymous 2014-07-09T15:49:45+00:00 Mark, No problem. I agree for your purposes that works just fine. The same process can be used to overcome the 25 item limit in legacy form fields: http://gregmaxey.mvps.org/word_tip_pages/formfield_userform_listbox.html 0 comments No comments
No problem. I agree for your purposes that works just fine. The same process can be used to overcome the 25 item limit in legacy form fields: http://gregmaxey.mvps.org/word_tip_pages/formfield_userform_listbox.html
Anonymous 2014-07-09T15:38:44+00:00 Hello Greg, Thank you for both responses. I have chosen to use the second technique as it is less combersome. Thank you for sharing your time and expertise. Mark 0 comments No comments
Thank you for both responses.
I have chosen to use the second technique as it is less combersome.
Thank you for sharing your time and expertise.
Anonymous 2014-07-07T23:29:08+00:00 Mark, Not using a dropdown formfield or a dropdown content control, but it is possible (but no walk in the sun) using a content control combobox and a whole lot of VBA code. Basically you have to create a custom change event (there is no build-in change event): See: http://gregmaxey.mvps.org/word_tip_pages/content_control_custom_events.html and the example to choose a castaway. 0 comments No comments
Not using a dropdown formfield or a dropdown content control, but it is possible (but no walk in the sun) using a content control combobox and a whole lot of VBA code.
Basically you have to create a custom change event (there is no build-in change event): See:
http://gregmaxey.mvps.org/word_tip_pages/content_control_custom_events.html and the example to choose a castaway.