Insert Multiple listbox selections to single bookmark

J

John Guderian

Ive got a userform, that displays a list of departments. I would like
someone to be able to make multiple selections, and place thos selections
into a single bookmark separated by commas. This bookmark is contained
within the header.

The current code I have to populate the listbox, and place the last selected
item into the bookmark is below.

Thanks for your help!

Private Sub UserForm_Initialize()
lstDepartments.List = Array("Accts. Payable", "Accts. Receivable",
"Service", "Rentals", "Warehouse")
End Sub

Private Sub CommandButton1_Click()
With lstDepartments
For x = 0 To .ListCount - 1
If .Selected(x) Then
UpdateBookmark "Text2", lstDepartments.List(x)
End If
Next
End With


Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
 
D

Doug Robbins - Word MVP

Private Sub CommandButton1_Click()
Dim Departments As String
Departments = ""
For x = 0 To lstDepartments.ListCount - 1
If .Selected(x) Then
Departments = Departments & ", " & lstDepartments.List(x)
End If
Next
Departments = Mid(Departments, 3) 'Strip the initial comma and space
UpdateBookmark "Text2", Departments


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

John Guderian

Doug - Thanks for your help. I actually had this working shortly after I
initially posted, but something happened, and I lost the entire document.

When I tried your code here, I got an error: Invalid or unqualified
reference when it gets to "If .Selected(x) Then

Is all I did to fix it was enter lstDepartment.Selected, and it worked fine.

I appreciate your help!
 
D

Default User

Use either of below two listings and look at the difference.

Listing 1)
For x = 0 To lstDepartments.ListCount - 1
If lstDepartments.Selected(x) Then
Departments = Departments & ", " & lstDepartments.List(x)
End If
Next

or use listing 2)
With lstDepartments
For x = 0 To .ListCount - 1
If .Selected(x) Then
Departments = Departments & ", " & .List(x)
End If
Next
End With

Krgrds,
Perry
 

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