Kay said:
Hello,
I have a form with a user form that is activated mid way through the
document. On the form is a combo box that list many items where each
entry has more than 255 characters. I use a macro to call up the
form. It all works great, thanks to the mvps.org.word posting on
user forms, but the end-users now tell me it must be a multi-select
which will then populate one text field. Is that possible and if so
how would I code the form? Do I have to use a list box or can you
make the combo box act the same way? Thanks for you help and advice.
Hi Kay,
A combo box doesn't have a MultiSelect property, so you will need to replace
it with a list box as you guessed. But a combo box is called that because
it's really a "combination" of a list box and an edit (text) box, so you can
get the same functionality by rolling your own combination. The list box is
the active part, and the text box serves to display the result of the
multiple selections.
I'll assume your combo box was set with .MatchRequired = True, so the user
can't type in new entries that aren't already in the list part of the box.
The rough equivalent in the list-plus-text box is to make the text box have
..Enabled = False so nothing can be typed there. If you allow .Enabled =
True, then the user could rearrange the selections or enter completely new
text. The choice is up to you.
In this sample, make up a userform (named frmMultiSelectList) with a text
box (tbxResult) that's several lines high, a list box (lbxChoices), and
command buttons (cmdOK and cmdCancel). Put this code in the userform:
Public bCancel As Boolean
Private Sub cmdCancel_Click()
bCancel = True
Me.hide
End Sub
Private Sub cmdOK_Click()
bCancel = False
Me.hide
End Sub
Private Sub lbxChoices_Change()
Dim strTemp As String
Dim idx As Long
strTemp = "" ' belt & suspenders
For idx = 0 To lbxChoices.ListCount - 1
If lbxChoices.Selected(idx) Then
If Len(strTemp) > 0 Then
strTemp = strTemp & " "
End If
strTemp = strTemp & lbxChoices.List(idx)
End If
Next idx
If Len(strTemp) > 0 Then
tbxResult.Text = strTemp
End If
End Sub
Private Sub UserForm_Initialize()
With tbxResult
.MultiLine = True
.Enabled = False
.TabStop = False
End With
' the data could be pulled from any source, e.g.,
' text file, AutoText entries, doc properties,
' Excel spreadsheet, database ...
With lbxChoices
.MultiSelect = fmMultiSelectMulti
.AddItem "My dog has fleas."
.AddItem "There's no fool like an old fool."
.AddItem "Never give a sucker an even break."
.AddItem "What color is your parachute?"
End With
End Sub
Then put this macro in an ordinary module, and select it as the entry macro
for each text formfield that will be filled from the list box:
Sub EnterField()
Dim dlg As frmMultiSelectList
Set dlg = New frmMultiSelectList
With dlg
.bCancel = False
.Show
If Not .bCancel Then
ActiveDocument.FormFields( _
Selection.Bookmarks(1).Name).Result = _
.tbxResult.Text
End If
End With
Set dlg = Nothing
End Sub
The most common requirement that isn't met by this setup is when the text
formfield should display the items in a different order than they occur in
the list box. That adds a whole new layer of complexity to the userform
code.
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.