VB and VBA Users Source Code: Copying selected items in a listview to the clipboard
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
VB/VBA Source Code
Copying selected items in a listview to the clipboard
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, August 19, 2002
Hits:
2384
Category:
Windows Forms/GUI/Controls/Graphics
Article:
The following code copies the selected items in a listview to the clipboard. 'Purpose : Copies the selected items in a listview to the clipboard 'Inputs : lvCopy The listview to copy the selected items from ' [bIncludeHeaders] If True the listview headers are also copied ' [lFirstCol] The first column to copy to the clipboard, ' if not specified copies all columns ' [lLastCol] The last column to copy to the clipboard, ' if not specified copies all columns ' [bCopyAll] If True will copy all the listview items. 'Outputs : Returns the number of items copied on success, else returns -1 'Author : Andrew Baker 'Date : 11/04/2001 'Notes : 'Revisions : Function LvCopyToClipboard(lvCopy As ListView, Optional bIncludeHeaders As Boolean, Optional lFirstCol As Long = 1, Optional lLastCol As Long = -1, Optional bCopyAll As Boolean = False) As Long Dim lThisRow As Long, lThisCol As Long, lNumCols As Long Dim sRow As String, sClipboard As String On Error GoTo ErrFailed With lvCopy.ListItems 'Loop over the items in the listview lNumCols = lvCopy.ColumnHeaders.count If lLastCol = -1 Then lLastCol = lNumCols End If If bIncludeHeaders Then sRow = "" For lThisCol = lFirstCol To lLastCol sRow = sRow & (lvCopy.ColumnHeaders.Item(lThisCol).Text & vbTab) Next sClipboard = sClipboard & (sRow & vbNewLine) End If For lThisRow = 1 To .count If .Item(lThisRow).selected Or bCopyAll = True Then 'Copy the selected item sRow = "" sRow = .Item(lThisRow).Text For lThisCol = lFirstCol To lLastCol - 1 sRow = sRow & (vbTab & .Item(lThisRow).SubItems(lThisCol)) Next sClipboard = sClipboard & (sRow & vbNewLine) LvCopyToClipboard = LvCopyToClipboard + 1 End If Next End With Clipboard.Clear DoEvents Clipboard.SetText sClipboard, vbCFText Exit Function ErrFailed: Debug.Print "Error copying to clipboard " & err.description Debug.Assert False LvCopyToClipboard = -1 End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder