?
Solved

Copy All Selected Items to Clipboard From Listbox

Posted on 2000-01-28
8
Medium Priority
?
244 Views
Last Modified: 2008-03-03
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.
0
Comment
Question by:RichW
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2397399
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
 
LVL 1

Expert Comment

by:sunil27
ID: 2397401
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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2397410
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 4

Author Comment

by:RichW
ID: 2413362
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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2413721
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
 
LVL 4

Author Comment

by:RichW
ID: 2415181
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
 
LVL 7

Accepted Solution

by:
Vbmaster earned 300 total points
ID: 2415882
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
 
LVL 4

Author Comment

by:RichW
ID: 2416141
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question