提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在Word文档中创建依赖下拉列表?


如左侧屏幕截图所示,您需要根据第一个下拉列表中的选项限制第二个下拉列表中的选项,您如何做? 本文将向您展示在Word文档中创建依赖下拉列表的方法。

使用VBA代码在Word中创建依赖的下拉列表


使用VBA代码在Word中创建依赖的下拉列表

以下VBA代码可以帮助您在Word文档中创建相关的下拉列表。 请按照说明一步一步。

1。 首先,您需要在Word文档中插入两个下拉列表。 点击 开发人员 > 遗产表格 > 下拉表格字段。 看截图:

2。 右键单击第一个下拉列表(此下拉列表应为父下拉列表),然后单击 属性。 见截图:

3。 在开幕式上 下拉表单字段选项 对话框中,您需要:

3.1在中输入类别 下拉项目 框,然后单击 添加 按钮,重复该操作,直到所有类别都添加到项目下拉列表框中。

3.2输入 ddfood书签 框。

3.3点击 OK 按钮。 看截图:

4。 右键单击第二个下拉列表,单击 楼盘 打开它的 下拉表单字段选项 对话框,然后在对话框中输入 ddCategory书签 框并单击 OK 按钮。 看截图:

5。 按 其他 + F11 键打开 Microsoft Visual Basic for Applications 窗口。

6。 在里面 Microsoft Visual Basic for Applications 窗口中,单击 插页 > 模块, 然后将下面的VBA代码复制到Module窗口中。

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. ddfood ddCategory 应该与您在上面两个中输入的书签选项相匹配 下拉表单字段选项 对话框。

7。 保存代码并返回到文档。

8。 右键单击第一个下拉列表,然后单击 楼盘 打开 下拉表单字段选项 对话框。 在对话框中,请从中选择上面的宏名称(此处为Popolateddfood) 出口 下拉列表,然后单击 OK 按钮。

9。 现在点击 开发人员 > 限制编辑 如下图所示。

10。 在里面 限制编辑 窗格,你需要:

10.1),检查 仅允许此类编辑 在文件中 框;

10.2)选择 填写表格 下拉列表中的选项;

10.3)点击 是的,开始执行保护 按钮;

10.4)在 开始执行保护 对话框,输入密码并单击 OK 按钮。 看截图:

现在创建了从属下拉列表。 从第一个下拉列表中选择Fruit时,只能在第二个下拉列表中选择水果类别。



推荐的Word生产力工具

Kutools For Word - 超过100 Word的高级功能,节省您的50%时间

  • 复杂和重复的操作可以在几秒钟内完成一次性处理。
  • 一次将多个图像跨文件夹插入Word文档。
  • 将多个文件夹合并并组合成一个具有所需订单的文件。
  • 根据标题,分节符或其他标准将当前文档拆分为单独的文档。
  • 在Doc和Docx,Docx和PDF之间转换文件,用于常见转换和选择的工具集合等等......
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.
    Amy · 1 months 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.
    Eduardo Gallardo · 1 months 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 · 1 months 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.
    SRMoritz · 3 months 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 months 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 · 3 months 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 months 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 · 4 months 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 months 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 · 6 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 · 6 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 · 5 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 · 7 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 · 6 months ago
      Good day,
      Sorry can help you with that. Thank you for your comment.