Popuate List in ComboBox

M

Matt

Hello. I am very new to VBA and would like to use a drop-down list for the
user to select an entry from the predetermined list. How to do I create the
list for the drop-down?
 
H

Hussain

Just type the control's name and a dot, the available property names
will be shown.

ComboBox1.AddItem "Apple"
ComboBox1.AddItem "Orange"
ComboBox1.AddItem "Carrot"
 
M

Matt

Thanks. Does the code below go between "Private Sub ComboBox1_Change ()" and
"End Sub"? That is what I did and yet I cannot get it to populate the
drop-down list.

I placed the ComboBox directly into a Word doc, not a UserForm. Is a
UserForm required?

Thanks again for your help!
 
J

Jay Freedman

Hi Matt,

A Userform is not required (although I tend to prefer them).

When you click the View Code button on the Control Toolbox while the combo
box is selected, what you get is misleading. You're being shown the code
procedure that will run when a user selects one of the items in the combo
box's list or types something into its box -- that's why the word "Change"
is in the procedure's name.

What you need to do instead is to write a procedure that runs when the
document is opened. To get that started, while the VBA editor is open, click
the dropdown at the top of the code window where it says "ComboBox1" and
choose "Document". VBA guesses wrong again and gives you a procedure named
Document_New (that's what runs when you use the File > New command to create
a new document from a template). Now click the other dropdown, which now
says "New", and select "Open". At last you have the procedure Document_Open
that you need.

Between "Private Sub Document_Open()" and "End Sub", put the kind of
statements that Hussain described.

You can delete the "Private Sub ComboBox1_Change()" and "Private Sub
Document_New()" lines and the "End Sub" lines that match them; or you can
just leave them there with no code inside them. Having an empty procedure is
almost the same as not having the procedure at all (it takes Word a few
milliseconds to "do nothing").

As a bit of background: Word doesn't save the items in a combo box's list
when it saves a document. Your code needs to rebuild that list every time
the document opens. That's why you have to use the Document_Open procedure.

For a lot more detail about the controls in the Toolbox, read
http://msdn2.microsoft.com/en-us/library/aa140269(office.10).aspx.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
P

Paul

Hello Jay,
I'm afraid I have found this thread which describes what I am also trying to
achieve and after following your instructions I have the following in my Code
display :-

Private Sub ComboBox1_Change()

End Sub

Private Sub Document_Open()
ComboBox1.AddItem "CL313"
ComboBox1.AddItem "CL317"
ComboBox1.AddItem "CL319"
ComboBox1.AddItem "CL322"
ComboBox1.AddItem "CL365"
End Sub

My list however is still not populating and I cannot see where I am going
wrong, could you help a little more please as I am quite new to this but it
would be very useful for me to learn.?

Using Word 2002 WIN XP SP3

Many Thanks
 
D

Doug Robbins - Word MVP

Private Sub Document_Open()
ComboBox1.AddItem "CL313"
ComboBox1.AddItem "CL317"
ComboBox1.AddItem "CL319"
ComboBox1.AddItem "CL322"
ComboBox1.AddItem "CL365"
End Sub

in the ThisDocument thing will populate ComboBox1 when the document is
Opened after Saving and Closing it.

--
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, originally posted via msnews.microsoft.com
 

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