PDA

View Full Version : ComboBox and Select Case



WallIT
01-21-2013, 09:44 AM
Hi

I am new to VBA and have a question about using a ComboBox and Select Case statement together.

I have a combobox which gets populated with about 30 names of staff members. Depending on the selection made by the user I need to update a few bookmarks with either "he" or "she".

Is the best solution to use a Select Case statement? If so, I can't work out how to do it.

How do I pass the combobox1.value to the select case statement?

Also, as there are less women than men in the list of 30, it makes sense to do something like...

select case = [any of the female names] then bookmark = "she"
case else bookmark = "he"

Thanks for any tips.

gmaxey
01-21-2013, 12:25 PM
What kind of combobox? Why are you using a combobox for a fixed list?

If is is a contentcontrol then something like this in the ThisDocument.Module:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Select Case ContentControl.Title
Case "Staff"
Select Case ContentControl.Range.Text
Case "Sara", "Jill"
FillBM "she", "Gender"
Case Else
FillBM "he", "Gender"
End Select
End Select
End Sub
Sub FillBM(ByRef strValue As String, strName As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(strName).Range
oRng.Text = strValue
ActiveDocument.Bookmarks.Add strName, oRng
End Sub

WallIT
01-22-2013, 03:02 AM
Hi Greg,

Thanks for your quick reply. As far as I am aware I am using a standard ComboBox, selected from the toolbox in VBA. Are there different types of ComboBox?

I thought this would be the most suitable control to use as I need users to select a name from a list of 30 and I would like the form to be quite compact in size. Is there a more suitable control?

I'm not sure what a contentcontrol is. I searched in the VBA help in Word, but nothing came up. I then looked at your website and read that contentcontrol was introduced in Word 2007. We are (still) using Word 2003 unfortunately.

Reading more, it seems contentcontrol would be perfect for the type of template I am creating, but that will have to wait until we upgrade later this year.

In the meantime, is it possible to do this with a ComboBox?

gmaxey
01-22-2013, 05:56 AM
Then I assume that you are using a combobox on a UserForm. Just be aware, that unless you set the properties correctly, the user can add their own name e.g., Mickey Mouse as free text in the control. What you need to do is built a multi-column combobox e.g.,

Jill she
Tammy she
Bill he
Tom he
Joan she

Display only the first column, but use both in your execute code.
http://gregmaxey.mvps.org/word_tip_pages/populate_userform_listbox_or_combobox.html