combo box won't display menu items

P

Pratchmg

I'm trying to put a combo box into a word document in VBA. This is my code

Private Sub ComboBox1_Change()
With ComboBox1
For ComboBox = 1 To 3
..AddItem "Mr"
..AddItem "Mrs"
..AddItem "Ms"
Next ComboBox
End With

End Sub

When I reopen the doccument, the menu items disappear. Would anyone have
any suggestions ?
 
K

Karl E. Peterson

Pratchmg said:
I'm trying to put a combo box into a word document in VBA. This is
my code

Private Sub ComboBox1_Change()
With ComboBox1
For ComboBox = 1 To 3
.AddItem "Mr"
.AddItem "Mrs"
.AddItem "Ms"
Next ComboBox
End With

End Sub

When I reopen the doccument, the menu items disappear. Would anyone
have any suggestions ?

It doesn't make sense to populate a list when the selection within changes,
as there can be no selection if there are no elements.

Why don't you populate the list in the Document_Open event instead?
 
J

Jay Freedman

The ComboBox control doesn't store any of its information in the document
file. You have to reload it every time you create or open the document. That
means your initialization code must be in the Document_New() and
Document_Open() procedures, not in the ComboBox1_Change() procedure.

To make maintenance a bit easier, put the code into a separate procedure (so
all the AddItem calls are only in one place) and call that procedure from
both Document_New() and Document_Open().

Also, your code is going to add three complete sets of abbreviations to the
ComboBox's list -- I don't think you really want to do that. Remove the For
and Next statements.

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

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

Similar Threads

list box remove item by combobox 4
Inserting text at a bookmark with a Userform 0
variables not keep their values 4
Populate combobox 6
listbox - questions 2
Combo Box Problem 2
Problem with Combo Box 5
Combo Boxes 2

Top