Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-10-2021, 08:41 AM
gmaxey gmaxey is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Word MVP 2003-2009
Programatically create Fully Functional DropdownList (or ComboBox) Content Controls
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,189
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default 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
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.


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
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.

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
Has anyone found a way to do this without having to use the built-in UI or a template?


Cross posted at:
Programatically create Fully Funcitional DropdownList (or ComboBox) Content Controls
Programatically create Fully Funcitional DropdownList (or - Microsoft Community
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by macropod; 01-10-2021 at 02:21 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 01-10-2021, 04:35 PM
Guessed's Avatar
Guessed Guessed is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 2,031
Guessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to all
Default

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: '
Reply With Quote
  #3  
Old 01-11-2021, 12:19 AM
gmayor's Avatar
gmayor gmayor is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,344
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

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
or take a look at Insert Content Control Add-In
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 01-11-2021, 02:13 AM
macropod's Avatar
macropod macropod is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,726
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

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]
Reply With Quote
  #5  
Old 01-11-2021, 05:45 AM
gmayor's Avatar
gmayor gmayor is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,344
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

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
Reply With Quote
  #6  
Old 01-11-2021, 08:29 AM
gmaxey gmaxey is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Word MVP 2003-2009
Programatically create Fully Functional DropdownList (or ComboBox) Content Controls
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,189
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 01-11-2021, 08:38 AM
gmaxey gmaxey is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Word MVP 2003-2009
Programatically create Fully Functional DropdownList (or ComboBox) Content Controls
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,189
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

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
Appears to work as well.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 01-15-2021, 09:36 AM
gmaxey gmaxey is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Word MVP 2003-2009
Programatically create Fully Functional DropdownList (or ComboBox) Content Controls
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,189
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 01-17-2021, 07:21 PM
Guessed's Avatar
Guessed Guessed is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 2,031
Guessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to all
Default

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
The other oddity I noticed is that you can't repeat the same line as you get an error message that says there is already a list item with that text value.
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
Reply With Quote
  #10  
Old Yesterday, 09:04 AM
gmaxey gmaxey is offline Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Windows 10 Programatically create Fully Functional DropdownList (or ComboBox) Content Controls Office 2016
Word MVP 2003-2009
Programatically create Fully Functional DropdownList (or ComboBox) Content Controls
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,189
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

Andrew,
Yes, there is certainly some mysteries involved here.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

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
Programatically create Fully Functional DropdownList (or ComboBox) Content Controls 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

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 07:20 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft