Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

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

Author Siluvia Last modified
Example showing limiting the choices in the second drop-down list based on the selected option in the first drop-down list

如截圖所示,您可能希望根據第一個下拉列表中的選擇來限制第二個下拉列表中的選項。本文將向您展示如何在 Word 文檔中創建相依的下拉列表。

使用 VBA 程式碼在 Word 中創建相依的下拉列表


使用 VBA 程式碼在 Word 中創建相依的下拉列表

以下的 VBA 方法可幫助您創建一個相依的下拉列表,其中第二個列表會根據第一個列表中的選擇而改變。請按照以下步驟操作:

  1. 確保 "開發工具" 選項卡在您的 Word 功能區中可見(如果不可見,請參閱教程 如何在 Word 中顯示開發工具選項卡?)。然後通過點擊 "開發工具" > "舊版表單" > "下拉表單欄位" 兩次來插入兩個下拉列表。
    Developer tab with Legacy Forms selected and Drop-Down Form Field highlighted
  2. 右鍵點擊第一個下拉列表(這將是父列表),然後點擊 "屬性"。
    First drop-down list right-clicked with Properties option selected
  3. 在 "下拉表單欄位選項" 對話框中:
    1. 將類別(例如,"水果", "蔬菜", "肉類")逐一輸入到 "下拉項目" 框中,每次輸入後點擊 "添加"。
    2. 在 "書籤窗格" 框中輸入 "ddfood"。
    3. 點擊 "確定"。
      Drop-down Form Field Options dialog box
  4. 右鍵點擊第二個下拉列表,選擇 "屬性",在 "書籤窗格" 框中輸入 "ddCategory",然後點擊 "確定"。
    Drop-down Form Field Options dialog box
  5. 按下 Alt + F11 打開 Microsoft Visual Basic for Applications 窗口。
  6. 在 VBA 窗口中,點擊 "插入" > "模組",然後粘貼以下程式碼:
    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

    注意:

    • 在程式碼中,更改每個 "Case" 語句下的項目以適應您的內容。
    • "ddfood" 和 "ddCategory" 必須與您在 "下拉表單欄位選項" 對話框中輸入的書籤名稱匹配。
  7. 保存程式碼並返回到您的文檔。
  8. 再次右鍵點擊第一個下拉列表,然後選擇 "屬性"。在出現的 "下拉表單欄位選項" 對話框中,從 "退出" 下拉列表中選擇宏名稱 "Populateddfood",然後點擊 "確定"。
    Drop-down Form Field Options dialog box
  9. 點擊 "開發工具" > "限制編輯"。
    Restrict Editing button on the Developer tab on the ribbon
  10. 在 "限制編輯" 窗格中:
    1. 勾選 "僅允許在文檔中進行此類型的編輯"。
    2. 從下拉列表中選擇 "填寫表單"。
    3. 點擊 "是的,開始強制保護"。
    4. 在 "開始強制保護" 對話框中,設置密碼並點擊 "確定"。
      Restrict Editing pane and Start Enforcing Protection dialog

現在,相依的下拉列表已啟用。當您在第一個列表中選擇 "水果" 時,第二個列表中只會出現水果選項。

Examples showing limiting the choices in the second drop-down list based on the selected option in the first drop-down list

最佳辦公效率工具

Kutools for Word -透過超過 100 項卓越功能,提升您的 Word 使用體驗!

🤖 Kutools AI FeaturesAI AssistantReal-Time AssistantSuper Polish(保留格式)Super Translate(保留格式)AI RedactionAI Proofread...

📘 文件掌握分割頁面文檔合併區域導出成各種格式(PDF/TXT/DOC/HTML...)批量轉換為 PDF...

正文編輯批量查找與替換多個文件批量調整所有圖片大小翻轉表格的行與列表格轉文本...

🧹 輕鬆清理:快速清除多餘空格分節符文字框超連結/更多清除工具請前往刪除工具組...

創意插入:插入千位分隔符複選框選項按鈕二維碼條形碼批量插入圖片/還有更多精彩功能在插入工具組...

🔍 精確選擇:精確定位特定頁面表格形狀標題段落/使用更多 選擇 功能提升導航效率...

亮點增強導航至任何區域自動插入重複文字切換文檔窗口11 轉換工具...

🌍 支援40+種語言:可用 Kutools於您偏好語言環境——支援英語、西班牙語、德語、法語、中文以及超過40種語言!

Kutools and Kutools Plus tabs on the Word Ribbon
👉 想立即體驗這些功能嗎?立即下載 Kutools for Word!🚀
 

✨ Kutools for Office——一次安裝,五大強力工具!

包含 Office Tab Pro · Kutools for Excel · Kutools for Outlook · Kutools for Word · Kutools for PowerPoint

📦 五大工具一次全擁有 | 🔗 與 Microsoft Office 無縫整合 | ⚡ 即刻省時提高工作效率

最佳辦公效率工具

Kutools for Word - 100+ 款 Word 工具