Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

如何在Word文檔中創建相關的下拉列表?


如左圖所示,您需要根據第一個下拉菜單中的選項來限制第二個下拉列表中的選擇,怎麼辦? 本文將向您展示在Word文檔中創建依賴下拉列表的方法。

使用VBA代碼在Word中創建相關的下拉列表


使用VBA代碼在Word中創建相關的下拉列表

下面的VBA代碼可以幫助您在Word文檔中創建相關的下拉列表。 請逐步按照說明進行操作。

1.首先,您需要在Word文檔中插入兩個下拉列表。 請點擊 開發人員 > 舊版表格 > 下拉式表格欄位。 看截圖:

2.右鍵單擊第一個下拉列表(此下拉列表應為父列表),然後單擊 屬性。 見截圖:

3.在開幕 下拉表單字段選項 對話框,您需要:

3.1將類別輸入 下拉項目 框,然後單擊 加入 按鈕,重複該操作,直到將所有類別添加到下拉列錶框中的項目中。

3.2輸入 吃貨書籤 框。

3.3點擊 OK 按鈕。 看截圖:

4.右鍵單擊第二個下拉列表,單擊 氟化鈉性能 打開它的 下拉表單字段選項 對話框,然後在對話框中輸入 dd類別書籤 框並單擊 OK 按鈕。 看截圖:

5。 按 其他 + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

6。 在裡面 Microsoft Visual Basic for Applications 窗口中,單擊 插入 > 模塊 然後將下面的VBA代碼複製到“模塊”窗口中。

VBA代碼:在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.在代碼中,請根據需要更改每種情況下的項目。

2. 吃貨dd類別 應該與您在上面兩個中輸入的“書籤”選項匹配 下拉表單字段選項 對話框。

7.保存代碼,然後返回文檔。

8.右鍵單擊第一個下拉列表,然後單擊 氟化鈉性能 打開 下拉表單字段選項 對話框。 在對話框中,請從上方選擇上面的宏名稱(此處為Popolateddfood)。 Exit 下拉列表,然後單擊 OK 按鈕。

9.現在點擊 開發人員 > 限制編輯 如下圖所示。

10。 在裡面 限制編輯 窗格,您需要:

10.1),檢查 只允許這種類型的編輯 在文件中 框;

10.2)選擇 填寫表格 從下拉列表中選擇;

10.3)點擊 是的,開始執行保護 按鈕;

10.4)在 開始執行保護 對話框中,輸入密碼,然後單擊 OK 按鈕。 看截圖:

現在,將創建從屬下拉列表。 從第一個下拉列表中選擇“水果”時,在第二個中只能選擇水果類別。


推薦的Word生產力工具

 

Kutools For Word - 100 多個 Word 高級功能,節省 50% 的時間

  • 複雜和重複的操作可以在幾秒鐘內完成一次處理。
  • 跨文件夾一次將多個圖像插入Word文檔。
  • 將文件夾中的多個Word文件合併並合併為所需順序。
  • 根據標題,分節符或其他條件將當前文檔拆分為單獨的文檔。
  • 在Doc和Docx之間,Docx和PDF之間轉換文件,用於常見轉換和選擇的工具集合等...
Say something here...
symbols left.
You are guest
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.
    enc · 1 months ago
    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?
  • To post as a guest, your comment is unpublished.
    Enc · 1 months ago
    Hi, thx for the detailed tutorial, but I have a question since the word template is for others' use, 
  • To post as a guest, your comment is unpublished.
    ray23 · 1 years ago
    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?
    • To post as a guest, your comment is unpublished.
      Richard Daniels · 9 months ago
      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.
  • To post as a guest, your comment is unpublished.
    Gmanny · 1 years ago
    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
    • To post as a guest, your comment is unpublished.
      Jeremy Whiting · 11 months ago
      I have the same issue has anyone been able to help on this one
  • To post as a guest, your comment is unpublished.
    مهسا · 2 years ago
    سلام من همه مراحل رو رفتم ولی درآخر وقتی از لیست اول یه استان رو انتخاب می کنم تویه لیست دوم فقط شهر های اون استان رو نمیاره
    چیکار باید بکنم؟
  • To post as a guest, your comment is unpublished.
    Silas Pickens · 2 years ago
    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?
    • To post as a guest, your comment is unpublished.
      Tino · 2 years ago
      I have the same problem!! :0
  • To post as a guest, your comment is unpublished.
    Amy · 2 years ago
    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
  • To post as a guest, your comment is unpublished.
    egdesign28@gmail.com · 2 years ago
    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"
  • To post as a guest, your comment is unpublished.
    Ed · 2 years ago
    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")
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Ed,
      Sorry can't help you with that. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    SRMoritz · 2 years ago
    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
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Good day,
      Sorry can't help you with that yet. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Paul · 2 years ago
    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?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Paul,
      Sorry can't help you with that yet. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Marc · 2 years 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.
      crystal · 2 years ago
      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.
  • To post as a guest, your comment is unpublished.
    mary · 2 years 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.
      crystal · 2 years ago
      Hi mary,
      Which version do you mean?
  • To post as a guest, your comment is unpublished.
    Ingrid · 2 years 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 · 2 years 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.
    elleleton · 2 years 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 years ago
      Good day,
      Sorry can help you with that. Thank you for your comment.