Copy All Selected Items to Clipboard From Listbox

I use the following code:

For i = 0 To List1.ListCount - 1

  ClipBoard.SetText List1.List(i)

Next i

This only copies the last item selected.  How can I copy ALL SELECTED items in the ListBox to the Clipboard?

Thanks in advance.
LVL 4
RichWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
Dim i As Integer
Dim sValue As String

For i = 0 To (List1.ListCount - 1)
  sValue = sValue & "," & List1.List(i)
Next

ClipBoard.SetText sValue
0
sunil27Commented:
You can store the list items in a string variable and then copy that string value to the clipboard, something like this:

Private Sub Command1_Click()
   
    Dim i As Integer
    Dim bFirst As Boolean
    Dim sBuff As String
   
    bFirst = True
    For i = 0 To List1.ListCount - 1
        If bFirst Then
            sBuff = List1.List(i)
        Else
            sBuff = sBuff & vbCrLf & List1.List(i)
        End If
        bFirst = False
    Next i
   
    Clipboard.Clear
    Clipboard.SetText sBuff
   
End Sub

0
VbmasterCommented:
This will copy a string with all the items with a "," character between each item. You can of course replace the "," string above with any other divider you may want to use. Using vbBack is often a good idea because this is the backspace character and won't be used in any of the items.

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

RichWAuthor Commented:
Sorry Sunil, but it doesn't grab all the selected items in the listbox.  It only grabs the first item.

The items I'm going after are email addresses, which have been parsed from a page of text and placed in the listbox.

VBMaster, your code grabs all the items in the listbox, and I substituted the "," with ";" for the delimited, or divider character, so we're getting closer.  

The problem is, I want to grab only the items "Selected" in the MultiLine set listbox.

My problem has been that I cannot find the Method or Property to refer to only Selected items.  .Text doesn't work, because you can't use the index array using ".Text"

Do you have an idea of how I can do this?  I really appreciate your help guys.
0
VbmasterCommented:
Hmm.. how can we know if a item in a Listbox is Selected.. let's see.. maybe there is a Selected{something} property... F1 -> Listbox -> Properties -> Wow a Selected property. *this looks harder than brain surgery* ;)

How about changing my code to something like this then..

   Dim i As Integer
   Dim sValue As String

   For i = 0 To (List1.ListCount - 1)
      If List1.Selected(i) Then
         sValue = sValue & "," & List1.List(i)
      End If
   Next

   ClipBoard.SetText sValue

You could make this harder by using a counter and see how many items you have included so far to see if this was the last selected item. The number of selected items can be found using the SelCount property. But.. ah well.. the listbox can't really contain that many items (less than 32k) so I do not think that you will see too much performance loss using this easier and shorter method.
0
RichWAuthor Commented:
You're not a doctor, but I bet you play one on the Net.

This worked, and I had to get rid of the leading ";" character.  The final code looks like this:

Private Sub Command1_Click()
   
Dim i As Integer
Dim sValue As String
Dim sChar As String

For i = 0 To (List1.ListCount - 1)
    If List1.Selected(i) Then
 
        sValue = sValue & ";" & List1.List(i)
 
    End If

Next

sChar = InStr(1, sValue, ";")
sValue = Mid(sValue, sChar + 1)

Clipboard.SetText sValue

End Sub


Answer the question and I'll give you the points.

Thanks
0
VbmasterCommented:
Just to have the answer contain something useful for ya...

I do not understand why you use the following code...

   sChar = InStr(1, sValue, ";")
   sValue = Mid(sValue, sChar + 1)

Since the first character is a ";" or the string is empty you could use code like...

   sValue = Mid$(sValue, 2)

(this will work even if the string is empty cause mid$(sValue, 2) will then return a empty string so no need to check if the length of the string is bigger than 0).

Closer to perfection. ;)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RichWAuthor Commented:
Thanks, Doc.  You're the Ginchiest!

I tried that already, but being the lessor mortal that I am I placed it in the If...Then statement and it removed multiple characters.  Duh.

Thanks for your help.

Rich
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.