PDA

View Full Version : Order of items in a listbox



Big_Shrimp
10-03-2013, 11:14 AM
Hello All,
I am using Word 2010 on a Windows 7 64-bit machine. I have created a template with a custom userform and included a Listbox control. I used the 1-fmMultiSelectMulti property on the listbox so that you can pick 1 or 2 of the 3 items.The listbox contains a list of names by order of the person's job title but when someone selects the names that they want to appear in the document, it seems the names come out in alphabetical order instead of job title order as I have them listed. Is there some property or code that I can use to get them to come out in the order that I have them listed as?

Example
My code reads:
Listbox1 "Smith, Pres."
Listbox1 "Doe, Sr. Vice Pres."
Listbox1 "Jones, Vice Pres."

On the Userform, it you were to select Smith and Jones, it would appear in the document as Jones and Smith. I want them to appear as Smith and Jones.

Thanks for any and all help.

Jay Freedman
10-03-2013, 08:10 PM
Selected items from a list box in a userform don't just magically appear in the document body. Presumably you have some code in the Click event of the OK button that transfers the text from the list box to the document, and that code determines the order in which the items are transferred. If you step through the list from top to bottom, transferring selected items as you come to them, they'll appear in that order in the document. Something like this:

Private Sub cmdOK_Click()
Dim index As Long
Dim strOut As String

For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
If Len(strOut) > 0 Then strOut = strOut & " and "
strOut = strOut & ListBox1.List(index)
End If
Next

ActiveDocument.Range.InsertAfter strOut
Me.Hide
End Sub

If your code is placing the selected items at a bookmark, one at a time, then the bookmark isn't being moved to the end of the current insertion before the code does the next insertion; the bookmark is just sitting at the beginning of the location while more text is stuffed in before the previously inserted text. It can be tricky to get that method correct.

fumei
10-03-2013, 10:29 PM
It may be helpful if you posted the code you are using to put the selected items into the document.