Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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 method to create dependent drop-down lists in 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 creating dependent drop-down lists in a Word document. Please follow the instruction 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 the dependent drop-down lists are created. When selecting Fruit from the first drop-down list, only the fruit categories can be selected in the second one.

Recommended Productivity Tools

shot kutools word kutools tab 1180x121
shot kutools word kutools plus tab 1180x120
 

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

  • Complicated and repeated operations can be done a one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders in to one with your desired order.
  • Split the current document into separate documents according to heading 1, 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...
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Marc · 11 days ago
    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!
  • To post as a guest, your comment is unpublished.
    mary · 2 months ago
    can this be done in older versions of Word or have to be saved as a certain doc type?
  • To post as a guest, your comment is unpublished.
    Ingrid · 2 months ago
    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..
    • To post as a guest, your comment is unpublished.
      crystal · 1 months ago
      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
  • To post as a guest, your comment is unpublished.
    MM · 3 months ago
    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?
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Good day,
      Sorry can help you with that. Thank you for your comment.