Skip to main content

How to create dependent drop-down lists in Word document?


As the left screenshot shown, you need to limit the choices in the second drop-down list based on the option in the first drop-down, how can you do? This article will show you a method to create dependent drop-down lists in a Word document.

Create dependent drop down lists in Word with VBA code


Create dependent drop down lists in Word with VBA code

The below VBA code can help you create a dependent drop-down list in a Word document. Please follow the instructions step by step.

1. Firstly, you need to insert two drop-down lists in your Word document. Click Developer > Legacy Forms > Drop-Down Form Field. See screenshot:

2. Right click the first drop-down list (this drop-down list should be the parent one), and click Properties. See screenshot:

3. In the opening Drop-down Form Field Options dialog box, you need to:

3.1 Enter the category into the Drop-down item box and then click the Add button, repeat the operation until all categories are added to the Items in drop-down list box.

3.2 Enter ddfood into the Bookmark box.

3.3 Click the OK button. See screenshot:

4. Right click the second drop-down list, click Properties to open its Drop-down Form Field Options dialog box, and in the dialog, enter ddCategory into the Bookmark box and click the OK button. See screenshot:

5. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

6. In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy below VBA code into the Module window.

VBA code: Create dependent drop-down list in Word

Sub Populateddfood()
'Update by Extendoffice 2018/10/25
    Dim xDirection As FormField
    Dim xState As FormField
    On Error Resume Next
    Set xDirection = ActiveDocument.FormFields("ddfood")
    Set xState = ActiveDocument.FormFields("ddCategory")
    If ((xDirection Is Nothing) Or (xState Is Nothing)) Then Exit Sub
    With xState.DropDown.ListEntries
        .Clear
        Select Case xDirection.Result
            Case "Fruit"
                .Add "Apple"
                .Add "Banana"
                .Add "Peach"
                .Add "Lychee"
                .Add "Watermelon"
            Case "Vegetable"
                .Add "Cabbage"
                .Add "Onion"
            Case "Meat"
                .Add "Pork"
                .Add "Beef"
                .Add "Mutton"
        End Select
    End With
End Sub

Notes:

1. In the code, please change items under each case as you need.

2. ddfood and ddCategory should match the Bookmark options you entered in the above two Drop-down Form Field Options dialog boxes.

7. Save the code and go back to the document.

8. Right click the first drop-down list and click Properties to open the Drop-down Form Field Options dialog box. In the dialog box, please select the above Macro name (here is Popolateddfood) from the Exit drop-down list, and then click the OK button.

9. Now click Developer > Restrict Editing as below screenshot shown.

10. In the Restrict Editing pane , you need to:

10.1) Check the Allow only this type of editing in the document box;

10.2) Select Filling in forms option from the drop-down list;

10.3) Click the Yes, Start Enforcing Protection button;

10.4) In the Start Enforcing Protection dialog, enter the password and click the OK button. See screenshot:

Now a dependent drop-down list is created. When selecting Fruit in the first drop-down list, only the fruit categories can be selected in the second one.


Recommended Word Productivity Tools

 

Kutools For Word - More Than 100 Advanced Features For Word, Save Your 50% Time

  • Complicated and repeated operations can be done one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders into one with your desired order.
  • Split the current document into separate documents according to heading, section break or other criteria.
  • Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
Comments (31)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi there,

I have managed to follow the steps, thanks for your help.

I can't find my code when pressing Alt+F11 though? My drop downs are still there but I can't find the back coding?
This comment was minimized by the moderator on the site
Hi Crystal, thanks for the tutorial, it was useful and well written. Is it possible to add a third dropdown list with options that change depending on what is selected from the second dropdown list?
This comment was minimized by the moderator on the site
Hi Jack,

You can add a third drop-down list with options that change depending on what is selected from the second drop-down list. But you need to insert another "Drop-Down Form Field" and a new VBA code in advance.
1. Follow the steps from 1 to 4 in the post to create the first and second drop-down list and modify the properties.
2. Insert the third Drop-Down Form Field, double click to open it's Properties dialog box, enter a name in the Bookmark textbox (the same as the operation for the first and second drop-down lists) , in this case, I made up a random name called ddTaste, and then click OK.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/word-drop-down1.png
3. Open the Microsoft Visual Basic for Applications window, enter the VBA code provided in the article into the Module1. Then insert a new Module (called Module2), copy and paste the following VBA code into this Module2.
VBA code: Use for the third dependent drop-down list

Sub PopulateddTaste()
Select Case ActiveDocument.FormFields("ddCategory").Result
Case "Apple"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "AA"
.Add "BB"
End With
Case "Banana"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "CC"
.Add "DD"
End With
Case "Peach"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "EE"
.Add "FF"
End With
Case "Lychee"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "GG"
.Add "HH"
End With
Case "Watermelon"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "II"
.Add "JJ"
End With
Case "Cabbage"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "LL"
.Add "MM"
End With
Case "Onion"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "OO"
.Add "PP"
End With
Case "Pork"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "QQ"
.Add "RR"
End With
Case "Beef"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "SS"
.Add "TT"
End With
Case "Mutton"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "UU"
.Add "VV"
End With
End Select
End Sub

Note: In the code, AA, BB, CC... are the items you want to show in the third drop-down list, based on what was selected from the second drop-down list. For example, when you select "Apple" in the second drop-down, the third drop-down list will display the items AA and BB. Please modify this items to meet your needs.
4. Save the code and go back to the document. Follow the step 8 in the article to specify the macro for the first drop-down.
5. Double click the second drop-down list, choose "PopulateddTaste" in the Exist drop-down list.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/word-drop-down2.png
6. Then follow the step 8 to restrict editing in this document.
Now you have create a three level drop-down list.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/word-drop-down3.gif
This comment was minimized by the moderator on the site
Thanks for getting back to me, your time and help is appreciated.
This comment was minimized by the moderator on the site
Hallo,

ist das ganze auch unter Outlook möglich. Leider bekomme ich das so nicht hin da die Eigenschaften des Dropdown Menüs ganz anders aussehen.

Ich wollte unter Aufgaben einen Reparaturauftrag erstellen wo man Gebäude,Bereich und dan Zimmernummer oder Bezeichnung auwählen kann.

Ist das möglich in Outlook/Aufgaben?



Danke
This comment was minimized by the moderator on the site
Hi,
You can create a drop-down list with custom field in Outlook Task window by following the steps in this article:
How To Add A Drop Down List With Custom Filed In Outlook Task Window?
https://www.extendoffice.com/documents/outlook/4453-outlook-custom-fields-tasks-drop-down.html
Or create a drop-down list in word then copy and paste it into the Task window as you need.
But if you want to create dependent drop-down lists in Outlook Task, no method has been found yet.
Sorry about that.
This comment was minimized by the moderator on the site
Hi, many thx for the detailed tutorial, but I have a question since the word template is for others' use. You mentioned protecting mode and a password is a must, this can do if I tell them the password, then there is a conflict: there are some other blanks which are needed to be filled in, this cannot be done only if the protecting mode is off. What should I do? Is there any method to meet both 2 needs?
This comment was minimized by the moderator on the site
Hi, thx for the detailed tutorial, but I have a question since the word template is for others' use, 
This comment was minimized by the moderator on the site
This was extremely helpful, thank you.
I have a question, is there a way to modify the code so that when I select a new item from the first drop-down list, the dependent one will clear up?
This comment was minimized by the moderator on the site
Sorry for the late reply, only stumbled upon this page today :-)
It already does that. The first statement inside the Select case ... End select construction clears the second list. If none of the criteria are met, nothing is added.
This comment was minimized by the moderator on the site
Hello, for some reason I cannot modify anything else in the word file if the drop down list is active. Is there a way to avoid that?

Thank you
This comment was minimized by the moderator on the site
I have the same issue has anyone been able to help on this one
This comment was minimized by the moderator on the site
سلام من همه مراحل رو رفتم ولی درآخر وقتی از لیست اول یه استان رو انتخاب می کنم تویه لیست دوم فقط شهر های اون استان رو نمیاره
چیکار باید بکنم؟
This comment was minimized by the moderator on the site
I am using this same code however the text that I replacing the ddcategory with has 100 plus words. Is there anyway to wrap the text to prevent it from going off of the page and disappearing?
This comment was minimized by the moderator on the site
I have the same problem!! :0
This comment was minimized by the moderator on the site
Thank you for this very useful resource.

Is it possible to repeat the same dependent drop down multiple times within a form?

I need the form filler to assign a category to each new row (but with the same options each time).I have successfully created the drop downs but when I restrict editing to test they seem to reset themselves as I work down the rows.

Thanks
Amy
This comment was minimized by the moderator on the site
Hello, How can i duplicate the categories box? I have used the code above and works well, but only for one box. What do I do in the code if for example if I need to select up to 3 fruits once I have selected the Fruit group? I am duplicating the field I created but only one works.
---------------------
Select
FRUIT: banana
apple
"select item"


----------------------------------
Select
Vegetable: Cabbage
onion
"select item"
This comment was minimized by the moderator on the site
Is there a way to add multiple category fields based on the one selection? for example I need say Select "Wine" but i need to be able to select 5 options (fruity, red, strong, etc...) from the 10 possible options in the second drop-down field list. I followed instructions above and works well on one field with one other dependent field only. But I need 5 dependent fields, all the same triggered by the one field in this case "wine". I duplicate the drop-down field it doesn't work, I am trying to add a second field to the code, but not sure how: ActiveDocument.FormFields("ddCategory") and ("ddCategory2")
This comment was minimized by the moderator on the site
Hi Ed,
Sorry can't help you with that. Thank you for your comment.
This comment was minimized by the moderator on the site
HI I got the drop downs to work. I have a question
When I make the selection for the first drop down, Is there any way to automate the coordinating and related selections for the following drop downs? For example. I have a parent drop down Attorney, the two child drop downs are Title and Phone number respectively. When I select the attorney name, I would like the related child drop downs to auto populate the title and phone number. How would I be able to do that?

Thanks in advance.

Sylvia
This comment was minimized by the moderator on the site
Good day,
Sorry can't help you with that yet. Thanks for your comment.
This comment was minimized by the moderator on the site
Hi,

How do we text wrap? I followed the step and it works fine except that when the second list is too long, it goes beyond the page. Is there a way to have it automatically wrap?
This comment was minimized by the moderator on the site
Hi Paul,
Sorry can't help you with that yet. Thanks for your comment.
This comment was minimized by the moderator on the site
Hi There,
are there restrictions with the name in the field (e.g. 2 words, or use of special caracters)?
Like Field A (Company Name) Field B (Director Name, like Jack Black).
Thanks!
This comment was minimized by the moderator on the site
Hi Marc,
Yes, there are restrictions with the name in the field.
For multiple words with spaces, you need to replace the spaces with undelines such as Company_Name.
And bookmark name can't contain any of the special characters such as / \ : * ? " < > |
Thanks for your comment.
This comment was minimized by the moderator on the site
can this be done in older versions of Word or have to be saved as a certain doc type?
This comment was minimized by the moderator on the site
Hi mary,
Which version do you mean?
This comment was minimized by the moderator on the site
Can you add this to multiple areas on a single page consisting of multiple sections? I was able to successfully complete the process in one section of my document, but when I attempt to complete the same process while on the same page, but in a different section of my document and with new drop down options, only my first added boxes are working, not the ones in the second section..
This comment was minimized by the moderator on the site
Hi ingrid,
Supposing you have three groups of dependent drop-down lists in your document. If you need all dependent drop-down lists take effect, please apply the below VBA code and do the below settings:

In the step 4 and 5 we mentioned in the article, now you need to do the following changes:
1. For the first group of the dependent drop-down lists, please get into each drop-down list's Properties window and specify the Bookmark as ddfood1 and ddCategory1 separately;
2. For the second group of the dependent drop-down lists, please get into each drop-down list's Properties window and specify the Bookmark as ddfood2 and ddCategory2 separately;
2. For the third group of the dependent drop-down lists, please get into each drop-down list's Properties window and specify the Bookmark as ddfood3 and ddCategory3 separately;

Then go ahead exactly as we provided in the article to finish the whole settings.

VBA code:
Sub Populateddfood()
'Update by Extendoffice 2019/03/18
Dim xDirection As FormField
Dim xState As FormField
Dim xRng As Range
Dim xFoodBM, xCategoryBM As String
Set xRng = Selection.Range
On Error Resume Next
For i = 1 To ActiveDocument.FormFields.Count
xFoodBM = "ddfood" & i
xCategoryBM = "ddCategory" & i
Set xDirection = ActiveDocument.FormFields(xFoodBM)
Set xState = ActiveDocument.FormFields(xCategoryBM)
If (Not (xState Is Nothing)) And (Not (xDirection Is Nothing)) Then
With xState.DropDown.ListEntries
.Clear
Select Case xDirection.Result
Case "Fruit"
.Add "Apple"
.Add "Banana"
.Add "Peach"
.Add "Lychee"
.Add "Watermelon"
Case "Vegetable"
.Add "Cabbage"
.Add "Onion"
Case "Meat"
.Add "Pork"
.Add "Beef"
.Add "Mutton"
End Select
End With
End If
Set xDirection = Nothing
Set xState = Nothing
Next
xRng.Select
End Sub
This comment was minimized by the moderator on the site
Is there a similar code to do the almost the exact same thing except have a text form field automatically populate depending upon the dropdown choice? For example, choosing a specific company in the dropdown and having a text form field automatically populate with the city where the company is located?
This comment was minimized by the moderator on the site
Good day,
Sorry can help you with that. Thank you for your comment.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations