Log in

View Full Version : Solved: Populating Combo boxs located in document



clhare
07-05-2007, 05:03 AM
I have some combo boxes that I need to include in the document text. I know I could populate them from "This Document", but what if I need to populate them using a userform instead? How do I reference the combo box from the userform?

clhare
07-05-2007, 10:59 AM
I figured out a way to get this to work (may not be the right way...)

I ran the userform from "This Document > Document_Open". Then in the "Document_Open" procedure after the userform is loaded and run, I populated the combox boxes by using something like the following:

Private Sub Document_Open()

Load frmMyForm
frmMyForm.Show

If frmMyForm.chk1.Value = True Then
cbo1.AddItem ("first item")
cbo1.AddItem ("second item")

cbo2.AddItem ("first item")
cbo2.AddItem ("second item")
end if

End Sub

fumei
07-06-2007, 10:16 AM
Huh? Yes this will work, but why are you doing this? What is the relationship?

In any case, the actual syntax to reference a ActiveX combobox is ThisDocument. From a Userform commandbutton:Private Sub CommandButton1_Click()
Dim myArray()
Dim var
myArray = Array("Hello", "World", _
"Item1", "Item2")
For var = 0 To UBound(myArray)
ThisDocument.ComboBox1.AddItem myArray(var)
Next
Unload Me
End SubYou may also reference an ActiveX control from ANY module using ThisDocument. From a standard module (NOT a userform module):Sub MakeIt()
Dim myArray()
Dim var
myArray = Array("Bob", "Harry", _
"Item1", "Item2")
ThisDocument.ComboBox1.Clear
For var = 0 To UBound(myArray)
ThisDocument.ComboBox1.AddItem myArray(var)
Next
End SubNote the use of .Clear If you run populating code on a control with AddItem, then it...adds items. So say you have a situation where you want to change the listed items. Then obviously you need to clear off existing items, THEN populate the combobox.