Skip to main content

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

Author: Siluvia Last Modified: 2025-08-06
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 -讓你的 Word進階,擁有超過 100 項強大功能!

🤖 Kutools AI FeaturesAI Assistant / 即時助手 / 超級潤色(保留格式) / 超級翻譯(保留格式) / AI遮擋 / AI校正...

📘 文檔掌控文檔拆分頁面 / 文檔合併 / 選取內容多格式導出(PDF/TXT/DOC/HTML...)/ 批量轉換為 PDF...

內容編輯跨多個文件批量查找與替換 / 圖片大小統一調整 / 行列翻轉表格 / 表格轉文本...

🧹 潔淨無憂:一鍵清除多餘空格 / 分節符 /文字方塊 / 超鏈接 / 更多清除工具請至 清除所有 群組...

創新插入:插入千位分隔符 / 複選框 / 選項按鈕 / 二維碼 / 條形碼 / 批量插入圖片 / 更多插入功能請前往 插入 群組...

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

優質強化輕鬆跳轉至文件夾 / 自動插入重複文字 / 切換文檔窗口 /11 轉換工具...

🌍 支援40+ 種語言:可用您的偏好語言體驗 Kutools —— 支援英語、西班牙語、德語、法語、中文等超過40 種語言!

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

最佳辦公效率工具

Kutools for Word - 100+ Word 工具