Add Combobox to Word document using VBA
The following code would add a Combo Box control to the existing Word document:
Sub Add_A_ContentControl() Dim oCC As ContentControl Set oCC = ActiveDocument.ContentControls.Add(wdContentControlComboBox, Selection.Range) oCC.SetPlaceholderText , , "Which Team Won the World Cup 2010" oCC.Title = "World Cup Teams" oCC.DropdownListEntries.Add "Spain", 1 oCC.DropdownListEntries.Add "Netherlands", 0 oCC.DropdownListEntries.Add "France", 2 oCC.DropdownListEntries.Add "Uruguay", 3 ' Prevents the Control from being deleted oCC.LockContentControl = True End Sub
Lock the control by setting the LockContentControl attribute to prevent it getting accidentally deleted.
The content control gets added as shown below
Great post!
ReplyDeleteI've been searching for this for a while!
ReplyDelete