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