Consulting

Results 1 to 4 of 4

Thread: Programatically create Fully Functional DropdownList (or ComboBox) Content Controls

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location

    Programatically create Fully Functional DropdownList (or ComboBox) Content Controls

    Has anyone had any success (without using a sledgehammer) for creating and insert fully functional dropdown list contentcontrols?

    A basic attempt (Attempt 1) produces a CC with the expected "Choose an item." displayed. But there is no "Choose an item." DD list entry, so no way for the user to re-select the null a null listing.

    Sub Attempt1()
    Dim oCC As ContentControl
      Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDropdownList, Selection.Range)
      With oCC
        .DropdownListEntries.Add "A", "Apples"
        .DropdownListEntries.Add "B", "Beets"
      End With
      'Result: "Choose an item." is displayed but there is no null "Choose an item." entry in the resulting DD List.
    lbl_Exit:
      Exit Sub
    End Sub

    With a little more grease, (Attempt 2) produces a CC with the expected "Choose an item." displayed and the expected "Choose an item." DD list entry, but if that entry is selected, the value displayed is the "text" variant of "Choose and item." not the expected null variant.




    Sub Attempt2()
    Dim oCC As ContentControl
      Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDropdownList, Selection.Range)
      With oCC
        .DropdownListEntries.Add oCC.PlaceholderText, vbNullString
        .DropdownListEntries.Add "A", "Apples"
        .DropdownListEntries.Add "B", "Beets"
        'Result: "Choose an item." is displayed and there is a "Choose a item." in the DD List. _
        However, if that item is selected, the display is not displayed as null Placeholder text."
      End With
    lbl_Exit:
      Exit Sub
    End Sub

    Now the sledgehammer. With (Attempt3), I first created a basic DDL content control with the built-in interface and saved it as a buildingblock. This works of course but requires access to a template.


    Sub Attempt3()
    Dim oRng As Range
    Dim oCC As ContentControl
      'Requires a DDL inserted via built-in UI saved as a buildingblock entry.
      Set oRng = Selection.Range
      ThisDocument.AttachedTemplate.BuildingBlockEntries("BasicDDL").Insert oRng, True
      oRng.End = oRng.End + 2
      Set oCC = oRng.ContentControls(1)
      With oCC
        .DropdownListEntries.Add "A", "Apples"
        .DropdownListEntries.Add "B", "Beets"
        'Result: A fully functional DDL content control.
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Has anyone found a way to do this without having to use the built-in UI or a template?

    Cross posted at:
    https://www.msofficeforums.com/word-vba/46303-programatically-create-fully-funcitional-dropdownlist-combobox-content.html#post156822
    https://answers.microsoft.com/en-us/...=1610293467362
    Last edited by macropod; 01-10-2021 at 02:21 PM. Reason: Added code tags & formatting
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Oct 2020
    Location
    Berlin, Germany. Often vivst Ukraine for business
    Posts
    6
    Location
    Thanks for update. I was looking for this.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    Bobby,

    A little more update if you care to look: https://www.msofficeforums.com/word-...x-content.html
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •