Combobox with For next Loop

S

Steven Cheng

sorry, i am not very good with VBA but I think this is possible. I want to
populate all comboboxes in a word document with the same options but do not
want to check for how many of them...I tried something like this...

Private Sub Document_Open()
dim cmb as combobox

For each cmb in ThisDocument
cmb.additem ""
cmb.additem 1
cmb.additem 2
cmb.additem 3
next
End Sub

I am getting an Object required error message on the ThisDocument statement
probably because I am not referencing the set of comboboxes in the
collection. However, Comboboxes OR Objects are not showing up as a valid
option.
can someone help me here. Thanks.
 
J

Jezebel

Comboboxes are OLE controls. These are in the Shapes collection; but there
are many other things that might be in that collection also. So you need to
iterate the collection and check the type of each member --

Dim pShape as Word.Shape

For each pShape in ActiveDocument.Shapes
if pShape.Type = msoOLEControlObject then 'It's a control
if pShape.OLEFormat.ClassType = "Forms.ComboBox.1" then
'It's a combobox
:
 
S

Steven Cheng

thanks Jezebel. works great.

Jezebel said:
Comboboxes are OLE controls. These are in the Shapes collection; but there
are many other things that might be in that collection also. So you need to
iterate the collection and check the type of each member --

Dim pShape as Word.Shape

For each pShape in ActiveDocument.Shapes
if pShape.Type = msoOLEControlObject then 'It's a control
if pShape.OLEFormat.ClassType = "Forms.ComboBox.1" then
'It's a combobox
:
 

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