View Single Post
 
Old 01-17-2021, 07:21 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,001
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
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