Seth Moster
Seth Moster

Reputation: 1

VBA Word: Create a dropdown ContentControl within a specific cell of a table and populate the dropdown

As far as I'm aware within a For loop this will add a row to the bottom of the intended Table.
Then finds the 2nd cell in the bottom most row.
Then adds a dropdown list and names it "Primary Role".
Then sets all the attributes to the contents of the dropdown.
Later it will loop back through here the number of times it takes to have the desired amount a rows.
So that every row has a copy of the same dropdown as the table grows longer.

'PersonnelTbl.Rows.Add
Set RoleCell = PersonnelTbl.Cell(PersonnelTbl.Rows.Count, 2).Range
With RoleCell.ContentControls.Add(wdContentControlDropdownList)
    .Title = "Primary Role"
    .SetPlaceHolderText , , "Role"
    .DropdownListEntries.Add "A"
    .DropdownListEntries.Add "B"
    .DropdownListEntries.Add "C"
    .DropdownListEntries.Add "D"
End With

The issue is these are considered a "Type mismatch":

.SetPlaceHolderText , , "Role"
.DropdownListEntries.Add "A"
.DropdownListEntries.Add "B"
.DropdownListEntries.Add "C"
.DropdownListEntries.Add "D"

If I expand it to it's full proper value of:

RoleCell.ContentControls.Add(wdContentControlDropdownList).SetPlaceHolderText , , "Role"

It returns as an "Invalid Command"

Do I "copy" the dropdown and paste it into the newly created Cell? Am I just specifying the drop down values wrong for the use case?

Upvotes: 0

Views: 137

Answers (1)

John Korchok
John Korchok

Reputation: 4913

I get an "Object required" error on your With RoleCell line. Here's a sample of working content control code:

Sub AddDropDownCC()
  Dim oCC As ContentControl
  Set oCC = ActiveDocument.ContentControls.Add(wdContentControlDropdownList, Selection.Range)
  With oCC
    .SetPlaceholderText Text:="Role"
    .DropdownListEntries.Add "Choose an item.", value:=""
    .DropdownListEntries.Add "Item 1", "Value 1"
    .DropdownListEntries.Add "Item 2", "Value 2"
  End With
  Set oCC = Nothing
End Sub

.DropdownListEntries.Add is expecting 2 strings, 1 for the displayed text and another for the value returned when that text is chosen.

Upvotes: 0

Related Questions