Copying/Pasting files to the Clipboard 
Author Message
 Copying/Pasting files to the Clipboard

I am having difficulty retrieving the file name as text from the clipboard.
Assuming a file was copied from explorer and is on the clipboard, I have
viewed the contents with the Clipboard Viewer and see the location/file name
as text there.  How do I retrieve it?  My code thus far, assigned to a menu
item

Private Sub PasteItem_Click()
    Dim MyItem, Msg
    If Clipboard.GetFormat(vbCFFiles) Then
        MyItem = Clipboard.GetText '(vbCFText)   ' Trying to get Clipboard
text.
        MsgBox MyItem
     Else
            Msg = "There is no file on the Clipboard."
            MsgBox Msg   ' Display error message.
    End If
End Sub

Thank you very much in advance... I've looked everywhere for some help in
this area.

Terry



Tue, 27 Mar 2001 03:00:00 GMT  
 Copying/Pasting files to the Clipboard
Getting that list of files "copied" from Explorer is not as easy as I
expected!  Here's a function that'll do it, though, and return a string
containing a comma-delimited list of file names.

It's probably not bullet-proof: hasn't been tested in NT, and it ignores
the DROPFILES .Offset (assumes it'll always be = 20): since they've
provided a value there, ya gotta be suspicious that it could change... It's
also got a hardcoded limit of 1024 characters, which you can change, but
it'll still be a hardcoded limit.

Anyway, I got this far and you can take it wherever you want to go
(today...) <g>

Jim Deutch
MS Dev MVP

Private Type DROPFILES
    Offset As Long
    x As Long
    y As Long
    flag As Long
    wide As Long
    files As String * 1024
End Type

Private Declare Function CloseClipboard% Lib "user32" ()
Private Declare Function OpenClipboard% Lib "user32" (ByVal hwnd%)
Private Declare Function GetClipboardData& Lib "user32" (ByVal wFormat As
Long)
Private Declare Function EnumClipboardFormats& Lib "user32" (ByVal wFormat
As Long)
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GlobalLock& Lib "kernel32" (ByVal hMem As Long)
Private Declare Function GlobalUnlock& Lib "kernel32" (ByVal hMem As Long)

Function GetClipboardFileList() As String
    Dim retval As Long
    Dim h As Long
    Dim h2 As Long
    Dim bFound As Boolean
    Dim DropInfo As DROPFILES
    Dim i As Long
    Dim nStop As Long
    Dim TwoZeros As String
    Dim OneZero As String
    Const CF_HDROP = 15

    TwoZeros = Chr$(0) & Chr$(0)
    OneZero = Chr$(0)
    retval = 0
    'find out if there are filenames on the clipboard
    If OpenClipboard(Form1.hwnd) Then
        Do
            retval = EnumClipboardFormats(retval)
            If retval = CF_HDROP Then
                bFound = True
            End If
        Loop While retval
        retval = CloseClipboard()
    End If

    If bFound Then
        If OpenClipboard(Form1.hwnd) Then
            'get the handle to the data
            h = GetClipboardData(CF_HDROP)
            'resolve the pointer
            h2 = GlobalLock(h)
            'copy the DROPFILES structure
            CopyMem DropInfo, ByVal h2, Len(DropInfo)
            'cleanup
            retval = GlobalUnlock(h)
            retval = CloseClipboard()

            For i = 1 To 1024
                'find the end of the list of files
                If Mid$(DropInfo.files, i, 2) = TwoZeros Then
                    nStop = i - 1
                    Exit For
                End If
                'change from null-delimited to comma-delimited
                If Mid$(DropInfo.files, i, 1) = OneZero Then
                    Mid$(DropInfo.files, i, 1) = ","
                End If
            Next i
        End If
    End If

    If nStop Then
        GetClipboardFileList = Left$(DropInfo.files, nStop)
    Else
        GetClipboardFileList = vbNullString
    End If

End Function



Quote:
> I am having difficulty retrieving the file name as text from the
clipboard.
> Assuming a file was copied from explorer and is on the clipboard, I have
> viewed the contents with the Clipboard Viewer and see the location/file
name
> as text there.  How do I retrieve it?  My code thus far, assigned to a
menu
> item



Tue, 27 Mar 2001 03:00:00 GMT  
 Copying/Pasting files to the Clipboard
Jim.

Works like a charm  :) .... Anyway to determine how many files have been
copied to the clipboard (ie. if more than one(1), do this, Else do something
else).

Thanks again  :)

Terry



Tue, 27 Mar 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Copying/Pasting files to the Clipboard II

2. RichTextBox <ctrl>V pastes 2 copies, <alt>EP pastes 1 copy

3. COPY AND PASTE FROM GLOBAL CLIPBOARD IN VBA

4. How to copy/paste a text to/from the Windows clipboard

5. How TO : Automatic copy (ctrl-c) + clipboard + paste (ctrl-v) + save as filename

6. Help - Copy And Paste to the Clipboard - HOW?

7. Efficient Copy/Paste Clipboard operations?

8. Require simple clipboard copy/paste help

9. Copy/Paste Clipboard in RTF Object?

10. How TO : Automatic copy (ctrl-c) + clipboard + paste (ctrl-v) + save as filename

11. RichText box control and Clipboard, cut, copy and paste

12. How TO : Automatic copy (ctrl-c) + clipboard + paste (ctrl-v) + save as filename

 

 
Powered by phpBB® Forum Software