How to reference an activex combobox on document

C

Cresta

I should know this, but could anyone let me know the code so from a Sub
Procedure, I can manually add a items to an activex combobox which is on a
document.

Thanks
 
D

Doug Robbins - Word MVP

Dim i As Long
Set myOB = ActiveDocument.Shapes _
.AddOLEControl(ClassType:="Forms.ComboBox.1")
With myOB.OLEFormat
.Activate
Set myObj = .Object
End With
With myObj
For i = 1 To 3
.AddItem "Item" & i
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
F

fumei via OfficeKB.com

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top