Populate ComboBox

D

DGjr.

Hi. I'm new to VBA and need to populate a ComboBox (more than 25 items). I
want it to populate only once upon opening the doc. Can someone help? I'm
tryint to use:

Private Sub ComboBox1_Change()
With ComboBox1
.AddItem "First Item"
.AddItem "Second Item"
End With
End Sub

Donnie
 
J

Jay Freedman

Hi. I'm new to VBA and need to populate a ComboBox (more than 25 items). I
want it to populate only once upon opening the doc. Can someone help? I'm
tryint to use:

Private Sub ComboBox1_Change()
With ComboBox1
.AddItem "First Item"
.AddItem "Second Item"
End With
End Sub

Donnie

You need to understand the idea of "events" and "event procedures".
Whenever the user clicks a different item in the combobox, the box's
Change event fires. Word then looks to see if there's a procedure
named ComboBox1_Change(), and executes it if it exists.

Since you want this code to execute when the document opens, you
picked the wrong event procedure to put it in. Instead, you want
Document_Open(), which executes when Word opens a document file.

All you need to do is change the first line of the procedure to

Private Sub Document_Open()

See http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm and
http://www.word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm for more
info.
 

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