چیکار باید بکنم؟
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.
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
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 Word Productivity Tools
- 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...
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 1 months agoسلام من همه مراحل رو رفتم ولی درآخر وقتی از لیست اول یه استان رو انتخاب می کنم تویه لیست دوم فقط شهر های اون استان رو نمیاره
چیکار باید بکنم؟
To post as a guest, your comment is unpublished.· 1 months agoI 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?
To post as a guest, your comment is unpublished.· 3 months agoThank 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.
To post as a guest, your comment is unpublished.· 3 months agoHello, 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.
To post as a guest, your comment is unpublished.· 3 months agoIs 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")
To post as a guest, your comment is unpublished.· 4 months agoHI 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.
To post as a guest, your comment is unpublished.· 5 months agoHi,
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?
To post as a guest, your comment is unpublished.· 6 months agoHi 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).
To post as a guest, your comment is unpublished.· 4 months agoHi 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.
To post as a guest, your comment is unpublished.
To post as a guest, your comment is unpublished.· 8 months agoCan 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.· 7 months agoHi 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.
'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
Select Case xDirection.Result
Set xDirection = Nothing
Set xState = Nothing
To post as a guest, your comment is unpublished.· 9 months agoIs 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?