Tip: andere talen zijn Google-Vertaald. Je kunt het English versie van deze link.
Log in
x
or
x
x
Registreren
x

or

Hoe afhankelijke vervolgkeuzelijsten in Word-document maken?


Zoals de linker schermafbeelding wordt getoond, moet u de keuzes in de tweede vervolgkeuzelijst beperken op basis van de optie in de eerste vervolgkeuzelijst, hoe kunt u dit doen? In dit artikel wordt de methode weergegeven om afhankelijke vervolgkeuzelijsten in een Word-document te maken.

Maak afhankelijke vervolgkeuzelijsten in Word met VBA-code


Maak afhankelijke vervolgkeuzelijsten in Word met VBA-code

De onderstaande VBA-code kan u helpen bij het maken van afhankelijke vervolgkeuzelijsten in een Word-document. Volg de instructies stap voor stap.

1. Allereerst moet u twee vervolgkeuzelijsten invoegen in uw Word-document. Klik ontwikkelaar > Legacy Forms > Vervolg formulierveld. Zie screenshot:

2. Klik met de rechtermuisknop op de eerste vervolgkeuzelijst (deze vervolgkeuzelijst moet de bovenliggende zijn) en klik op Eigenschappen. Zie screenshot:

3. In de opening Vervolg Formulierveldopties dialoogvenster, moet u:

3.1 Voer de categorie in in Vervolg item in en klik op de Toevoegen knop, herhaalt u de bewerking totdat alle categorieën zijn toegevoegd aan de vervolgkeuzelijst Items in.

3.2 Enter ddfood in de Bladwijzer doos.

3.3 Klik op de OK knop. Zie screenshot:

4. Klik met de rechtermuisknop op de tweede vervolgkeuzelijst en klik WONINGAANBOD om zijn Vervolg Formulierveldopties dialoogvenster en voer in het dialoogvenster in ddCategory in de Bladwijzer in en klik op de OK knop. Zie screenshot:

5. druk de anders + F11 toetsen om de te openen Microsoft Visual Basic voor toepassingen venster.

6. In de Microsoft Visual Basic voor toepassingen venster klikt bijvoegsel > module, kopieer vervolgens de VBA-code naar het modulevenster.

VBA-code: maak afhankelijke vervolgkeuzelijst in 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

Opmerkingen:

1. Wijzig de items in de code onder elk geval naargelang uw behoeften.

2. ddfood en ddCategory moet overeenkomen met de bladwijzeropties die u in de bovenstaande twee hebt ingevoerd Vervolg Formulierveldopties dialoogvensters.

7. Sla de code op en ga terug naar het document.

8. Klik met de rechtermuisknop op de eerste vervolgkeuzelijst en klik op WONINGAANBOD om de te openen Vervolg Formulierveldopties dialoog venster. Selecteer in het dialoogvenster de bovenstaande macronaam (hier is Popolateddfood) uit de afrit vervolgkeuzelijst en klik vervolgens op OK knop.

9. Klik nu ontwikkelaar > Beperken bewerken zoals hieronder screenshot getoond.

10. In de Beperken bewerken deelvenster, moet u:

10.1), Controleer de Sta alleen dit type bewerking toe in het document doos;

10.2) Selecteer Formulieren invullen optie uit de vervolgkeuzelijst;

10.3) Klik op de Ja, Start Enforcing Protection knop;

10.4) In de Begin met het afdwingen van bescherming dialoogvenster, voer het wachtwoord in en klik op de OK knop. Zie screenshot:

Nu worden de afhankelijke vervolgkeuzelijsten gemaakt. Wanneer u Fruit selecteert in de eerste vervolgkeuzelijst, kunnen alleen de fruitcategorieën in de tweede worden geselecteerd.



Aanbevolen Word-productiviteitstools

Kutools For Word - meer dan 100 geavanceerde functies voor Microsoft Word, sla uw 50% tijd op

  • Gecompliceerde en herhaalde bewerkingen kunnen eenmalig worden uitgevoerd in seconden.
  • Voeg meerdere afbeeldingen in meerdere mappen tegelijk in Word-documenten in.
  • Voeg meerdere Word-bestanden samen en combineer ze tot één map met de door u gewenste volgorde.
  • Splits het huidige document in afzonderlijke documenten volgens kop 1, sectie-einde of andere criteria.
  • Converteer bestanden tussen Doc en Docx, Docx en PDF, verzameling tools voor algemene conversies en selectie, en zo verder ...
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 · 18 days 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 · 27 days 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 · 27 days 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 · 2 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 · 1 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 · 2 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 · 1 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 · 3 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 · 1 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 · 5 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 · 4 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 · 6 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.