Link to home
Start Free TrialLog in
Avatar of RichW
RichWFlag for United States of America

asked on

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.
Avatar of Vbmaster
Vbmaster

Dim i As Integer
Dim sValue As String

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

ClipBoard.SetText sValue
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

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.

Avatar of RichW

ASKER

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.
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.
Avatar of RichW

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Vbmaster
Vbmaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RichW

ASKER

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