Multi-Select User Form

K

Kay

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.

Kay
 
J

Jay Freedman

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.
 
H

Helmut Weber

Hi Kay,

there is no multiselect property for a combobox.

On how to use the multiselect property with listboxes
there are some good examples in the help.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

Kay

Jay,

I tried your coding and with a few minor adjustments, it worked great.
Now...here is another challenge...I need each selection appear on a sseparate
line(techncally, a paragraph). Is there any way you can add a line of code
or just concatenate a paragraph symbol with each selection. Your input will
be graciously appareciated.
 
J

Jay Freedman

Hi Kay,

No problem, just a very small change. Replace the line

strTemp = strTemp & " "

with

strTemp = strTemp & vbCr

Instead of separating the items with a space, they'll now be separated with
a paragraph mark.

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

Kay

Jay,
The code replacement does what you explained in the text box of the user
form, but not in the text field of the document.

I tried vbNewLine as well, and that works in the textbox of the
userform...not in the text field of the document. What am I doing wrong. I
copied it in as you wrote it, typed it in...etc.

Kay
 
D

Doug Robbins - Word MVP

use

ActiveDocument.FormFields("[FormFieldBookmarkName]").Result = strTemp

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Kay

Doug,

Thanks but I am not quite clear on where you want this...in the enter field
macro or the code behind the form? Is the syntax of the code exact?

Kay
Doug Robbins - Word MVP said:
use

ActiveDocument.FormFields("[FormFieldBookmarkName]").Result = strTemp

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Kay said:
Jay,
The code replacement does what you explained in the text box of the user
form, but not in the text field of the document.

I tried vbNewLine as well, and that works in the textbox of the
userform...not in the text field of the document. What am I doing wrong.
I
copied it in as you wrote it, typed it in...etc.

Kay
 
J

Jay Freedman

Sorry, I should have remembered that little wrinkle. :-(

A carriage return (vbCr) in a text box on a userform actually consists
of two characters, Chr$(13) & Chr$(10). When the content of the text
box is copied into a formfield in the document, the formfield
translates the Chr$(13) into a paragraph mark, and the Chr$(10) shows
up as a square block at the start of the next line.

The solution is to go into the EnterField macro in the regular module
and change this statement

ActiveDocument.FormFields( _
Selection.Bookmarks(1).Name).Result = _
.tbxResult.Text

to this, which removes the Chr$(10) characters:

ActiveDocument.FormFields( _
Selection.Bookmarks(1).Name).Result = _
Replace(.tbxResult.Text, Chr$(10), "")

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

Kay

Jay,

Once again thanks!

Kay

Jay Freedman said:
Sorry, I should have remembered that little wrinkle. :-(

A carriage return (vbCr) in a text box on a userform actually consists
of two characters, Chr$(13) & Chr$(10). When the content of the text
box is copied into a formfield in the document, the formfield
translates the Chr$(13) into a paragraph mark, and the Chr$(10) shows
up as a square block at the start of the next line.

The solution is to go into the EnterField macro in the regular module
and change this statement

ActiveDocument.FormFields( _
Selection.Bookmarks(1).Name).Result = _
.tbxResult.Text

to this, which removes the Chr$(10) characters:

ActiveDocument.FormFields( _
Selection.Bookmarks(1).Name).Result = _
Replace(.tbxResult.Text, Chr$(10), "")

--
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

Top