Combo Box Problem

A

abs

In Word 2003, I created a form template. I created a macro that opens a user
form when the document is opened. The user form comes up and I can fill in
all the text fields, click OK and the document fills in everything exactly
where I want (where the bookmarks are located in the form). I used Dian
Chapman’s instructions and it works fine. All is well...except, I need three
drop downs (I assume combo boxes) from which the end user can select an item
from a list. I began by creating a ComboBox, but can’t seem to get it to
populate. I’ve seen several ways to accomplish this and can’t seem to get
any of them to work. I was going with:

Private Sub UserForm_Initialize()
With ComboBox1
.AddItem “oneâ€
.AddItem “twoâ€
.Add Item “threeâ€
End With
End Sub

Where exactly do I put that? I have all the information for my bookmarks,
etc., after btnUserOK_Click() and it works just fine.
 
G

Gordon Bentley-Mix

abs,

The UserForm initialisation code can go anywhere in the code behind the
UserForm; however, for convenience and easy of readability, I usually make it
the first sub after the declarations. In fact, what I do when I have multiple
ComboBoxes to populate is create separate subs for initialising each ComboBox
and call those subs in the initialisation code for the UserForm - something
like this:

Private Sub UserForm_Initialize()
PopulateSalutationsCombo
PopulateClosingsCombo
End Sub

Private Sub PopulateSalutationsCombo()
With cboSalutation
.AddItem "[SELECT]"
.AddItem "Dear..."
.AddItem "Dear Sir/Madam"
.AddItem "To Whom It May Concern"
.AddItem "Other..."
.AddItem "None"
End With
End Sub

Private Sub PopulateClosingsCombo()
With cboClosing
.AddItem "[SELECT]"
.AddItem "Yours sincerely"
.AddItem "Yours faithfully"
.AddItem "Regards"
.AddItem "Kind regards"
.AddItem "None"
End With
End Sub

If you have lots of items to put into the list (or if you just want a more
elegant solution), you can also use something like this:

Private Sub PopulateSalutationsCombo()
Dim myArray As String
myArray = Split("[SELECT]|Dear...|Dear Sir/Madam|To Whom It May
Concern|Other...|None", "|")
cboSalutation.List = myArray
End Sub

The disadvantage of this second approach, however, is that you cannot
specify the index of the items as they are added to the list; the items are
added in the order in which they occur in the array. (However, I didn't do
specify the index in the first approach anyway, so it's probably not much of
an issue.)

To get the selected value from the ComboBox to appear in the document, use
the .Value property of the ComboBox - something like this:

ActiveDocument.Bookmarks("Salutation").Text = cboSalutation.Value

Unfortunately, this doesn't explain why the code you posted doesn't seem to
work for you. To my eye, it looks as if it should. Is it perhaps just a
matter of not knowing where this code should be located? If so then the first
line of my post should give you an answer.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
A

abs

You're the bestest! Sorry I'm just getting back to you...I was out for a few
days and just got back to this project. Thank you!

Gordon Bentley-Mix said:
abs,

The UserForm initialisation code can go anywhere in the code behind the
UserForm; however, for convenience and easy of readability, I usually make it
the first sub after the declarations. In fact, what I do when I have multiple
ComboBoxes to populate is create separate subs for initialising each ComboBox
and call those subs in the initialisation code for the UserForm - something
like this:

Private Sub UserForm_Initialize()
PopulateSalutationsCombo
PopulateClosingsCombo
End Sub

Private Sub PopulateSalutationsCombo()
With cboSalutation
.AddItem "[SELECT]"
.AddItem "Dear..."
.AddItem "Dear Sir/Madam"
.AddItem "To Whom It May Concern"
.AddItem "Other..."
.AddItem "None"
End With
End Sub

Private Sub PopulateClosingsCombo()
With cboClosing
.AddItem "[SELECT]"
.AddItem "Yours sincerely"
.AddItem "Yours faithfully"
.AddItem "Regards"
.AddItem "Kind regards"
.AddItem "None"
End With
End Sub

If you have lots of items to put into the list (or if you just want a more
elegant solution), you can also use something like this:

Private Sub PopulateSalutationsCombo()
Dim myArray As String
myArray = Split("[SELECT]|Dear...|Dear Sir/Madam|To Whom It May
Concern|Other...|None", "|")
cboSalutation.List = myArray
End Sub

The disadvantage of this second approach, however, is that you cannot
specify the index of the items as they are added to the list; the items are
added in the order in which they occur in the array. (However, I didn't do
specify the index in the first approach anyway, so it's probably not much of
an issue.)

To get the selected value from the ComboBox to appear in the document, use
the .Value property of the ComboBox - something like this:

ActiveDocument.Bookmarks("Salutation").Text = cboSalutation.Value

Unfortunately, this doesn't explain why the code you posted doesn't seem to
work for you. To my eye, it looks as if it should. Is it perhaps just a
matter of not knowing where this code should be located? If so then the first
line of my post should give you an answer.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.


abs said:
In Word 2003, I created a form template. I created a macro that opens a user
form when the document is opened. The user form comes up and I can fill in
all the text fields, click OK and the document fills in everything exactly
where I want (where the bookmarks are located in the form). I used Dian
Chapman’s instructions and it works fine. All is well...except, I need three
drop downs (I assume combo boxes) from which the end user can select an item
from a list. I began by creating a ComboBox, but can’t seem to get it to
populate. I’ve seen several ways to accomplish this and can’t seem to get
any of them to work. I was going with:

Private Sub UserForm_Initialize()
With ComboBox1
.AddItem “oneâ€
.AddItem “twoâ€
.Add Item “threeâ€
End With
End Sub

Where exactly do I put that? I have all the information for my bookmarks,
etc., after btnUserOK_Click() and it works just fine.
 

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