Bỏ qua nội dung chính

Làm cách nào để tạo danh sách thả xuống phụ thuộc trong tài liệu Word?


Như ảnh chụp màn hình bên trái được hiển thị, bạn cần giới hạn các lựa chọn trong danh sách thả xuống thứ hai dựa trên tùy chọn trong danh sách thả xuống đầu tiên, bạn có thể làm như thế nào? Bài viết này sẽ chỉ cho bạn một phương pháp để tạo danh sách thả xuống phụ thuộc trong tài liệu Word.

Tạo danh sách thả xuống phụ thuộc trong Word với mã VBA


Tạo danh sách thả xuống phụ thuộc trong Word với mã VBA

Mã VBA dưới đây có thể giúp bạn tạo danh sách thả xuống phụ thuộc trong tài liệu Word. Vui lòng làm theo hướng dẫn từng bước.

1. Đầu tiên, bạn cần chèn hai danh sách thả xuống trong tài liệu Word của mình. Nhấp chuột Nhà phát triển > Biểu mẫu kế thừa > Trường biểu mẫu thả xuống. Xem ảnh chụp màn hình:

2. Nhấp chuột phải vào danh sách thả xuống đầu tiên (danh sách thả xuống này phải là danh sách chính) và nhấp Tính chất. Xem ảnh chụp màn hình:

3. Trong phần mở đầu Tùy chọn trường biểu mẫu thả xuống hộp thoại, bạn cần:

3.1 Nhập danh mục vào Mục thả xuống hộp và sau đó nhấp vào Thêm , lặp lại thao tác cho đến khi tất cả các danh mục được thêm vào hộp danh sách thả xuống Mục trong danh sách.

3.2 Nhập ddfood vào Bookmark cái hộp.

3.3 Nhấp vào OK cái nút. Xem ảnh chụp màn hình:

4. Nhấp chuột phải vào danh sách thả xuống thứ hai, nhấp vào Bất động sản để mở Tùy chọn trường biểu mẫu thả xuống hộp thoại và trong hộp thoại, hãy nhập ddThể loại vào Bookmark và nhấp vào OK cái nút. Xem ảnh chụp màn hình:

5. Nhấn nút Khác + F11 phím để mở Microsoft Visual Basic cho các ứng dụng cửa sổ.

6. bên trong Microsoft Visual Basic cho các ứng dụng cửa sổ, nhấp Chèn > mô-đun, sau đó sao chép mã VBA bên dưới vào cửa sổ Mô-đun.

Mã VBA: Tạo danh sách thả xuống phụ thuộc trong 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

Ghi chú:

1. Trong mã, xin vui lòng thay đổi các mục theo từng trường hợp khi bạn cần.

2. ddfoodddThể loại phải khớp với các tùy chọn Dấu trang mà bạn đã nhập trong hai tùy chọn trên Tùy chọn trường biểu mẫu thả xuống những hộp thoại.

7. Lưu mã và quay lại tài liệu.

8. Nhấp chuột phải vào danh sách thả xuống đầu tiên và nhấp vào Bất động sản để mở Tùy chọn trường biểu mẫu thả xuống hộp thoại. Trong hộp thoại, vui lòng chọn tên Macro ở trên (ở đây là Popolateddfood) từ Ra danh sách thả xuống, sau đó bấm vào OK .

9. Bây giờ hãy nhấp vào Nhà phát triển > Hạn chế Chỉnh sửa như ảnh chụp màn hình dưới đây.

10. bên trong Hạn chế Chỉnh sửa ngăn, bạn cần:

10.1) Kiểm tra Chỉ cho phép loại chỉnh sửa này trong tài liệu cái hộp;

10.2) Chọn Điền vào những mẫu đơn tùy chọn từ danh sách thả xuống;

10.3) Nhấp vào Có, Bắt đầu Thực thi Bảo vệ Nút;

10.4) Trong Bắt đầu thực thi bảo vệ , nhập mật khẩu và nhấp vào OK cái nút. Xem ảnh chụp màn hình:

Bây giờ một danh sách thả xuống phụ thuộc được tạo. Khi chọn Trái cây trong danh sách thả xuống đầu tiên, chỉ có thể chọn danh mục trái cây trong danh sách thứ hai.

Công cụ năng suất văn phòng tốt nhất

Kutools cho Word - Nâng cao trải nghiệm sử dụng từ của bạn với Over 100 Các tính năng đáng chú ý!

🤖 Trợ lý AI của Kutools: Biến đổi bài viết của bạn bằng AI - Tạo nội dung  /  Viết lại văn bản  /  Tóm tắt tài liệu  /  Hỏi thông tin dựa trên tài liệu, tất cả đều có trong Word

📘 Làm chủ tài liệu: Chia trang  /  Hợp nhất tài liệu  /  Lựa chọn xuất khẩu ở nhiều định dạng khác nhau (PDF/TXT/DOC/HTML...)  /  Chuyển đổi hàng loạt sang PDF  /  Xuất trang dưới dạng hình ảnh  /  In nhiều tập tin cùng một lúchữu ích. Cảm ơn !

Chỉnh sửa nội dung: Tìm và thay thế hàng loạt trên nhiều tệp  /  Thay đổi kích thước tất cả hình ảnh  /  Hoán đổi hàng và cột của bảng  /  Chuyển bảng thành văn bảnhữu ích. Cảm ơn !

🧹 Dễ dàng làm sạch: Quét đi Không gian bổ sung  /  Phần ngắt  /  Tất cả các tiêu đề  /  Hộp văn bản  /  Các siêu liên kết  / Để biết thêm các công cụ xóa, hãy truy cập Xóa nhómhữu ích. Cảm ơn !

Phụ trang quảng cáo: Chèn Ngàn dấu phân cách  /  Hộp kiểm tra  /  Nút radio  /  Mã QR  /  Mã vạch  /  Bảng đường chéo  /  Chú thích phương trình  /  Hình ảnh Caption  /  Bảng chú thích  /  Nhiều Hình Ảnh  / Khám phá thêm trong Chèn nhómhữu ích. Cảm ơn !

???? Lựa chọn chính xác: Xác định trang cụ thể  /  bảng  /  hình dạng  /  đoạn văn tiêu đề  / Tăng cường điều hướng với chi tiết Chọn tính nănghữu ích. Cảm ơn !

Cải tiến sao: Điều hướng nhanh chóng đến bất kỳ vị trí nào  /  tự động chèn văn bản lặp đi lặp lại  /  chuyển đổi liền mạch giữa các cửa sổ tài liệu  /  11 công cụ chuyển đổihữu ích. Cảm ơn !

👉 Bạn muốn thử những tính năng này? Kutools cho Word cung cấp một 60 ngày dùng thử miễn phí, không có giới hạn! 🚀
 
Comments (32)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi there,

I have managed to follow the steps, thanks for your help.

I can't find my code when pressing Alt+F11 though? My drop downs are still there but I can't find the back coding?
This comment was minimized by the moderator on the site
Hi Crystal, thanks for the tutorial, it was useful and well written. Is it possible to add a third dropdown list with options that change depending on what is selected from the second dropdown list?
This comment was minimized by the moderator on the site
Hi Jack,

You can add a third drop-down list with options that change depending on what is selected from the second drop-down list. But you need to insert another "Drop-Down Form Field" and a new VBA code in advance.
1. Follow the steps from 1 to 4 in the post to create the first and second drop-down list and modify the properties.
2. Insert the third Drop-Down Form Field, double click to open it's Properties dialog box, enter a name in the Bookmark textbox (the same as the operation for the first and second drop-down lists) , in this case, I made up a random name called ddTaste, and then click OK.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/word-drop-down1.png
3. Open the Microsoft Visual Basic for Applications window, enter the VBA code provided in the article into the Module1. Then insert a new Module (called Module2), copy and paste the following VBA code into this Module2.
VBA code: Use for the third dependent drop-down list

Sub PopulateddTaste()
Select Case ActiveDocument.FormFields("ddCategory").Result
Case "Apple"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "AA"
.Add "BB"
End With
Case "Banana"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "CC"
.Add "DD"
End With
Case "Peach"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "EE"
.Add "FF"
End With
Case "Lychee"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "GG"
.Add "HH"
End With
Case "Watermelon"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "II"
.Add "JJ"
End With
Case "Cabbage"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "LL"
.Add "MM"
End With
Case "Onion"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "OO"
.Add "PP"
End With
Case "Pork"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "QQ"
.Add "RR"
End With
Case "Beef"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "SS"
.Add "TT"
End With
Case "Mutton"
With ActiveDocument.FormFields("ddTaste").DropDown.ListEntries
.Clear
.Add "UU"
.Add "VV"
End With
End Select
End Sub

Note: In the code, AA, BB, CC... are the items you want to show in the third drop-down list, based on what was selected from the second drop-down list. For example, when you select "Apple" in the second drop-down, the third drop-down list will display the items AA and BB. Please modify this items to meet your needs.
4. Save the code and go back to the document. Follow the step 8 in the article to specify the macro for the first drop-down.
5. Double click the second drop-down list, choose "PopulateddTaste" in the Exist drop-down list.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/word-drop-down2.png
6. Then follow the step 8 to restrict editing in this document.
Now you have create a three level drop-down list.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/word-drop-down3.gif
This comment was minimized by the moderator on the site
Thanks for getting back to me, your time and help is appreciated.
This comment was minimized by the moderator on the site
Hallo,

ist das ganze auch unter Outlook möglich. Leider bekomme ich das so nicht hin da die Eigenschaften des Dropdown Menüs ganz anders aussehen.

Ich wollte unter Aufgaben einen Reparaturauftrag erstellen wo man Gebäude,Bereich und dan Zimmernummer oder Bezeichnung auwählen kann.

Ist das möglich in Outlook/Aufgaben?



Danke
This comment was minimized by the moderator on the site
Hi,
You can create a drop-down list with custom field in Outlook Task window by following the steps in this article:
How To Add A Drop Down List With Custom Filed In Outlook Task Window?
https://www.extendoffice.com/documents/outlook/4453-outlook-custom-fields-tasks-drop-down.html
Or create a drop-down list in word then copy and paste it into the Task window as you need.
But if you want to create dependent drop-down lists in Outlook Task, no method has been found yet.
Sorry about that.
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
Hi, thx for the detailed tutorial, but I have a question since the word template is for others' use, 
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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.
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
I have the same issue has anyone been able to help on this one
This comment was minimized by the moderator on the site
سلام من همه مراحل رو رفتم ولی درآخر وقتی از لیست اول یه استان رو انتخاب می کنم تویه لیست دوم فقط شهر های اون استان رو نمیاره
چیکار باید بکنم؟
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
I have the same problem!! :0
This comment was minimized by the moderator on the site
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
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations