Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Listbox order reversed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amy - 12 Oct 2006 23:34 GMT
Hi, I am creating a multi-pick listbox in a userform. The listbox has names
in alphabetical order. I am using the code below to import selected items
into the Word doc. Why does the list of selected items appear in the Word doc
in reverse alpha order? For example, my listbox has 2 choices "Apples"  and
"Oranges". If I pick both "Apples" and "Oranges" my Word doc will list the
choices as Oranges and Apples. I'd like the order in the Word doc to be
alphabetical as well. Thanks for any help you can provide!

For i = 1 To Team.ListCount
   '..and check whether each is selected
   If Team.Selected(i - 1) Then
       ' If selected, display item in msgbox
       ActiveDocument.Bookmarks("Team").Range.InsertBefore Team.List(i - 1)
& " "
   End If
Next
Greg Maxey - 12 Oct 2006 23:53 GMT
Amy,

If you step through your code you will see that you first insert "apples"
before the bookmark range and then you insert oranges before the bookmark
range which is before apples.

You could build a string first and then just insert it at the end of the
code:

Private Sub CommandButton1_Click()
Dim i As Long
Dim myString As String
For i = 1 To Team.ListCount
   '..and check whether each is selected
   If Team.Selected(i - 1) Then
       ' If selected, display item in msgbox
        myString = myString + Team.List(i - 1) & " "
   End If
Next
ActiveDocument.Bookmarks("Team").Range.InsertBefore myString
Me.Hide
End Sub

Or just process the ListCount is reverse order:

Private Sub CommandButton1_Click()
Dim i As Long
For i = Team.ListCount To 1 Step -1
   '..and check whether each is selected
   If Team.Selected(i - 1) Then
       ' If selected, display item in msgbox
       ActiveDocument.Bookmarks("Team").Range.InsertBefore Team.List(i - 1)
& " "
   End If
Next
Me.Hide
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi, I am creating a multi-pick listbox in a userform. The listbox has
> names in alphabetical order. I am using the code below to import
[quoted text clipped - 13 lines]
>    End If
> Next
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.