Userform/FormFields

J

Jeff

Greetings,
I have a userform with a lisbox on it that allows multiple selections. I
would like to add FormFields for each of the selected items and add the
selected items text as the form field result property following a button
click event. Is this possible and any Ideas how to accomplish this? TIA.
 
F

Fumei2 via OfficeKB.com

Yes, it is possible. May I ask why you are using a text formfield to hold
the selected items text? Formfields are for user input, not to hold text.
If the user is not going to be changing the text - therefore changing the
text of the selected item - do not use a formfield.

Second question. WHERE do you want to put the new formfields? One right the
other, butting up to each other?

Try recording a macro inserting a formfield, and look at the code.

As for getting the selected items, something like:

For j = 0 To ListBox1.ListCount
If ListBox1.Selected(j) = True Then
ReDim Preserve MyList(l)
MyList(l) = ListBox1.List(j)
l = l + 1
End If
Next

This builds an array of the selected items. Now you can use the array to
insert new formfields. Use a formfield object. Something like:

Dim NewFormField As FormField

For k = 0 To Ubound(MyList())
' insert a formfield.....where??
Set NewFormField = Selection.FormFields.Add(Range:=Selection.Range, _
Type:=wdFieldFormTextInput)
With NewFormField
.Result = MyList(k) ' the current selected item from the array
.Name = "NewOne" & k
End With
Next
 
J

Jeff

Fumei, Thanks for the reply. When I posted this question I was attempting to
use a single Formfield to hold several lines of text. After some research and
some trial and error I came up with:

Sub AddToDocument(ByVal aString As String, ByVal I As Integer)
Dim ff As FormField
Set ff = ActiveDocument.FormFields.Add(Selection.Range,
wdFieldFormTextInput)
With ff
.Name = "Document" & I
.Result = aString & vbCr
End With
End Sub

Allowing me to add a FormField for each line of text passed to the procedure.
 

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