|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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. Code:
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 Code:
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 Code:
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 Cross posted at: Programatically create Fully Funcitional DropdownList (or ComboBox) Content Controls Programatically create Fully Funcitional DropdownList (or - Microsoft Community Last edited by macropod; 01-10-2021 at 02:21 PM. Reason: Added code tags |
#2
|
||||
|
||||
I think your clue of using the built-in UI provides an 'almost' acceptable method.
Code:
Sub Attempt4() Dim oCC As ContentControl Application.CommandBars.ExecuteMso ("ContentControlDropDownList") Set oCC = Selection.Range.Paragraphs(1).Range.ContentControls(1) 'this line is a weak attempt that could be more elegant With oCC .DropdownListEntries.Add "A", "Apples" .DropdownListEntries.Add "B", "Beets" End With lbl_Exit: Exit Sub End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia Last edited by Guessed; 01-10-2021 at 04:39 PM. Reason: ' |
#3
|
||||
|
||||
What you need is
Code:
Sub Macro1() Dim oCC As ContentControl Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDropdownList, Selection.Range) With oCC .Title = "My List Box" .Tag = .Title .LockContentControl = False .SetPlaceholderText , , "Select Fruit" .DropdownListEntries.Add Text:=.PlaceholderText, value:="" .DropdownListEntries.Add Text:="Apples", value:="Apples" .DropdownListEntries.Add Text:="Pears", value:="Pears" .DropdownListEntries.Add Text:="Plums", value:="Plums" .DropdownListEntries.Add Text:="Grapes", value:="Grapes" End With Set oCC = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
||||
|
||||
Graham: your code produces essentially the same result of Greg's Attempt2. The problem with that solution is that, unlike a Dropdown Content Control inserted via the GUI, the placeholder text is formatted the same as any other selected item instead of being the grey colour of the placeholder text one gets via the GUI.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
That's not what I see here. The placeholder text when displayed is light grey, both initially and when reselected
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
Guess,
I didn't see the tree for the forest. Thanks. I've modified a bit as Attempt5 because with your Attempt4, an error would occur if the CC just created is not the first indexed in the selection paragraph. Code:
Sub Attempt5() Dim oRng As Range Dim oCC As ContentControl Set oRng = Selection.Range Application.CommandBars.ExecuteMso ("ContentControlDropDownList") 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 |
#7
|
|||
|
|||
Paul, Graham
Graham appears to be correct. It seems my habit of using vbNullString in code has bitten me here. Revising my earlier Attempt2: Code:
Sub Attempt5() Dim oCC As ContentControl Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDropdownList, Selection.Range) With oCC.DropdownListEntries .Add oCC.PlaceholderText, "" 'Using vbNullString in this Attempt2 seems to have been the problem. .Add "A", "Apples" .Add "B", "Beets" End With lbl_Exit: Exit Sub End Sub |
#8
|
|||
|
|||
Graham, Paul, Andrew
I've still been futzing around with this process and came across another oddity shown in the first procedure below. After noodling out what caused (not why) a simple reorder of the code works. I've included a couple of methods that all seem to work and the most basic is the adaptation of Graham's. It seems to work just as well as the other with less code: Any of you have a guess as to what causes that OOM error? Code:
Option Explicit Sub AttemptUtterFailure() Dim oRng As Range Dim oCC As ContentControl Set oRng = Selection.Range Application.CommandBars.ExecuteMso ("ContentControlDropDownList") oRng.End = oRng.End + 2 Set oCC = oRng.ContentControls(1) With oCC .SetPlaceholderText , , "Select fruit" 'Attempt to redefine default "Choose an item." null entry results in RTE 4103 - Out of memory error. .DropdownListEntries.Item(1).Text = "Choose fruit from list." .DropdownListEntries.Item(1).Value = "" .DropdownListEntries.Add "Apples", "Apples" .DropdownListEntries.Add "Blueberries", "Beets" End With lbl_Exit: Exit Sub End Sub Sub AttemptSuccess1() Dim oRng As Range Dim oCC As ContentControl Set oRng = Selection.Range Application.CommandBars.ExecuteMso ("ContentControlDropDownList") oRng.End = oRng.End + 2 Set oCC = oRng.ContentControls(1) With oCC .SetPlaceholderText , , "Select fruit" 'Simply changing order of the following two lines seems to work and avoids the error. .DropdownListEntries.Item(1).Value = "" .DropdownListEntries.Item(1).Text = "Choose fruit from list." .DropdownListEntries.Add "Apples", "Apples" .DropdownListEntries.Add "Blueberries", "Beets" 'Result: A fully functional DDL content control. End With lbl_Exit: Exit Sub End Sub Sub AttemptSuccess2() Dim oRng As Range Dim oCC As ContentControl Set oRng = Selection.Range Application.CommandBars.ExecuteMso ("ContentControlDropDownList") oRng.End = oRng.End + 2 Set oCC = oRng.ContentControls(1) With oCC .SetPlaceholderText , , "Select fruit" 'Or we can delete the default "Choose an item" null entry and create a new one. .DropdownListEntries.Item(1).Delete .DropdownListEntries.Add "Choose fruit from list.", "" .DropdownListEntries.Add "Apples", "Apples" .DropdownListEntries.Add "Blueberries", "Beets" 'Result: A fully functional DDL content control. End With lbl_Exit: Exit Sub End Sub Sub AttemptSuccess3() Dim oRng As Range Dim oCC As ContentControl 'CCs added in this manner don't have the default "Choose an item" null entry. Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDropdownList, Selection.Range) With oCC .SetPlaceholderText , , "Select fruit" 'Add "Choose ..." null entry." .DropdownListEntries.Add "Choose fruit from list.", "" ' .DropdownListEntries.Add "Apples", "Apples" .DropdownListEntries.Add "Blueberries", "Beets" 'Result: A fully functional DDL content control. End With lbl_Exit: Exit Sub End Sub |
#9
|
||||
|
||||
Greg
I don't know where the error is coming from but interestingly, before the OOM error message shows, the code actually changes the list text value. Code:
Sub AttemptSuccess3() Dim oRng As Range Dim oCC As ContentControl Set oRng = Selection.Range Application.CommandBars.ExecuteMso ("ContentControlDropDownList") oRng.End = oRng.End + 2 Set oCC = oRng.ContentControls(1) With oCC .SetPlaceholderText , , "Select fruit" On Error Resume Next .DropdownListEntries.Item(1).Text = "Choose fruit from list." 'works anyway On Error GoTo 0 .DropdownListEntries.Item(1).Value = "" .DropdownListEntries.Add "Apples", "Apples" .DropdownListEntries.Add "Blueberries", "Beets" End With lbl_Exit: Exit Sub End Sub Code:
Sub AttemptSuccess4() Dim oRng As Range Dim oCC As ContentControl Set oRng = Selection.Range Application.CommandBars.ExecuteMso ("ContentControlDropDownList") oRng.End = oRng.End + 2 Set oCC = oRng.ContentControls(1) With oCC .SetPlaceholderText , , "Select fruit" 'Simply changing order of the following two lines seems to work and avoids the error. .DropdownListEntries.Item(1).Value = "" .DropdownListEntries.Item(1).Text = "Choose fruit from list." 'this works once .DropdownListEntries.Item(1).Text = "Choose fruit from list." 'errors if run a second time .DropdownListEntries.Add "Apples", "Apples" .DropdownListEntries.Add "Blueberries", "Beets" 'Result: A fully functional DDL content control. End With lbl_Exit: Exit Sub End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
Andrew,
Yes, there is certainly some mysteries involved here. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to save docx to doc that checks compatibility and converts content controls to static content. | staicumihai | Word VBA | 4 | 10-12-2016 08:23 PM |
Pass combobox content to header | wpryan | Word VBA | 3 | 07-17-2015 01:44 AM |
VBA ppt ComboBox behaivor: 1.OnFocus change value 2. get dropdownlist doesn't disappear after mousec | janism22 | PowerPoint | 2 | 03-26-2015 12:35 AM |
Create UserForm ComboBox or ContentControl | ptmuldoon | Word VBA | 11 | 01-17-2015 05:58 PM |
How to use the content of a combobox for {IF} field? | Melaanie | Word | 0 | 06-14-2010 02:00 AM |