Or, in the ThisDocument module:
Sub PopulateCombo()
Dim i As Long
With ComboBox1
.Clear
For i = 1 To 3
.AddItem "item" & i
Next
.ListIndex = 0
End With
End Sub
You can use just the name of the combobox within the ThisDocument module.
Replace Combobox1 with the actual name of the combobox. If you want to add
them on opening the document, put your code into the Document_Open event,
also in the ThisDocument module.
Generally, you want to have the first item in the list showing. You use
ListIndex = 0 to do this. It is also a good idea to Clear the list. If you
don't, then any code adding to the list does just that...it adds to the list.
You can also make your listed item dynamic, if you want. This allows quite a
bit of flexibility. As a sample, following on from the above:
Sub ComboBox1_Change()
Dim NewList As Variant
Dim i As Long
NewList = Array("doDah1", "doDah2", "doDah3")
Select Case ComboBox1.Text
Case "item3"
With ComboBox1
.Clear
For i = 0 To UBound(NewList)
.AddItem NewList(i)
Next
.ListIndex = 0
End With
Case "doDah2"
With ComboBox1
.Clear
For i = 1 To 3
.AddItem "item" & i
Next
.ListIndex = 0
End With
Case Else
' other choices as you like
End Select
End Sub
The combobox is originally populated as "item1", "item2", "item3". If
"item3" is selected, the combobox Change event fires. It them checks the
text value of the combobox. If it IS "item3", it clears the list, and
repopulates it with "doDah1", "doDah2", "doDah3".
If the value is "doDah2" - say after you have changed it to the doDah array -
then it will change the back to the "item" list.
By having flexible lists (from arrays) you can use one combobox for a number
of things.
Say the original list is:
Apples
Oranges
Bananas
Pears
If the user selects "Apples", you can re-populate the list with:
"MacIntosh"
"Delicious"
"Gala"
And then follow through with more logic from THAT list.
Or, use it to populate a different combobox, or whatever.