I am a bit of a novice when it comes VBA (and coding generally) and apologize in advance if this isn't possible. I have created a userform for users to input different pieces of information (name, address, etc.). When the user clicks the "OK" command button, the values of the differing text boxes appear at various bookmarks within the Word document. For instance, TextBox 1 is dedicated to name, TextBox2 is for the address, etc.
Additionally on the userform, I have added a ListBox that lists various types of data. Ideally, I'd like to have the multiple selections from that ListBox appear at the bookmark in Word on the same line together after clicking the "OK" command button on the userform. It transfers the values to the document (where the cursor is blinking), but I can't figure out how to assign it to the bookmark.
You can see below that I'm assigning the values of the textboxes to the bookmarks and essentially want to do the same with the ListBox.
The ListBox is filled at initialization with the following code.
With ListBox1
.AddItem "name"
.AddItem "address"
.AddItem "secondary address"
.AddItem "Social Security Number"
.AddItem "financial account number"
.AddItem "date of birth"
.AddItem "email address"
End With
End Sub
Private Sub CommandButton1_Click()
Dim DataSub1 As Range
Set DataSub1 = ActiveDocument.Bookmarks("DataSub1").Range
DataSub1 = Me.DataSub1.Value
Dim dAddress As Range
Set dAddress = ActiveDocument.Bookmarks("dAddress").Range
dAddress.Text = Me.dAddress.Value
Dim ii As Integer
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
Selection.Text = ListBox1.List(ii)
Selection.MoveRight
Selection.TypeParagraph
End If
Next ii
Application.ScreenRefresh
Me.Repaint
Me.Hide
End Sub
For example:
Dim ii As Integer, listText As String
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
listText = listText & ListBox1.List(ii) & vbCr
End If
Next ii
ActiveDocument.Bookmarks(bmkName).Range.Text = listText
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.