Results 1 to 12 of 12

Thread: Getting the path to a file saved on the windows clipboard

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45

    Getting the path to a file saved on the windows clipboard

    If i select a file in windows and the right click and click on copy. How can my program access the data contained on the clipboard about that file in order for me to get the path of the file?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    use the clipboard object within vb ....

    clipboard.getdata

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    I've tried that. All i get is a series of numbers that i have no idea what they mean.

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    ^bump^

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    I actually meant clipboard.gettext, not clipboard.getdata...... but that dont work either. so i dunno :/

  6. #6
    Junior Member
    Join Date
    Feb 2003
    Location
    Sweden
    Posts
    27
    Try this if you want to get the copied file(s)

    Code:
    'clipboard
    Private Const CF_TEXT = 1
    Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
    Private Declare Function CloseClipboard Lib "user32" () As Long
    Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Long, ByVal ByteLen As Long)
    
    
    ' Put the filepaths of copied files to a string
    Public Function ClipboardGetFiles(sDelimiter As String) As String
    Dim drop_handle As Long
    Dim num_file_names As Long
    Dim fileNames As String
    Dim file_name As String * 1024
    Dim i As Long
        fileNames = ""
        ' Make sure there is file data.
        If Clipboard.GetFormat(vbCFFiles) Then
            ' File data exists. Get it.
            ' Open the clipboard.
            If OpenClipboard(0) Then
                ' The clipboard is open.
    
                ' Get the handle to the dropped list of files.
                drop_handle = GetClipboardData(15)
    
                ' Get the number of dropped files.
                num_file_names = DragQueryFile(drop_handle, -1, vbNullString, 0)
    
                ' Get the file names.
                ReDim file_names(1 To num_file_names) As String
                For i = 1 To num_file_names
                    ' Get the file name.
                    DragQueryFile drop_handle, i - 1, file_name, Len(file_name)
    
                    ' Truncate at the NULL character.
                    'fileNames = fileNames & sDelimiter & """" & Left$(file_name, InStr(file_name, vbNullChar) - 1) & """"
                    fileNames = fileNames & sDelimiter & Left$(file_name, InStr(file_name, vbNullChar) - 1)
                Next
    
                ' Close the clipboard.
                CloseClipboard
    
                ' Assign the return value.
                ClipboardGetFiles = Trim(fileNames)
            End If
        End If
    End Function
    
    
    Private Sub Form_Load()
    'use as follow
    msgbox ClipboardGetFiles(", ")
    end sub

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    Thanks a bunch henrikf for the code. Can u believe i actually tried to do it using those exact same api's but couldn't get it to work. However while i was waiting i actually managed to do what i wanted a completly differant way. I ended up just using the oledragdrop function as it has an inbuilt declaration to get the file path. It integrated into my code fine however it relies on the code throwing an error. Does anyone know how i can clean this code up?

    VB Code:
    1. Private Function GetFileList(Data As DataObject)
    2. On Error GoTo got_them_all
    3. Dim i As Integer
    4. Dim s As String
    5. i = 1
    6.  
    7. Do
    8.     s = s & Data.Files(i) & "|"
    9.     i = i + 1
    10. Loop
    11.  
    12. got_them_all:
    13. GetFileList = s
    14. End Function

    if no one can then ill use ur code for sure henrikf. Sorry that the code is not immediatly usefull but i may be, so thanks a lot for the help

  8. #8
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    OK, this is how i use the function for ole drag / drop (Note: you need to set ur mdi form / controls OLE DROP MODE to manual

    VB Code:
    1. Private Sub MDIForm_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, Y As Single)
    2.     ' if there has been any file's dropped onto the form / control
    3.     If Data.Files.Count > 0 Then
    4.         Dim i As Long
    5.         ' loop through the files which have been dropped onto the form / control
    6.         ' to see if any of them are valid
    7.         For i = 1 To Data.Files.Count
    8.             ' if the file is not a .tbp file, dont do anything with it
    9.             If ((GetAttr(Data.Files(i)) And vbDirectory) = vbDirectory) Or _
    10.                (Right$(LCase(Data.Files(i)), 4) <> ".tbp") Then
    11.                 ' directory / not .tbp file, don't bother with it
    12.             Else
    13.                 ' it is a profile ext file, so try and load it,
    14.                 ' and if it fails to load, beep
    15.                 If LoadProfile(Data.Files(i), False) = False Then Beep
    16.             End If
    17.         Next i
    18.     End If
    19. End Sub
    20.  
    21. Private Sub MDIForm_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, Y As Single, State As Integer)
    22. On Error GoTo ErrHand:
    23.  
    24.     ' Don't allow dropping by default
    25.     Effect = vbDropEffectNone
    26.     ' if it is a list of files being dragged
    27.     If Data.GetFormat(vbCFFiles) Then
    28.         ' and there are some files being dragged onto the control / form
    29.         If Data.Files.Count > 0 Then
    30.             ' check to see if any of the files are a .tbp file
    31.             Dim i As Long
    32.             For i = 1 To Data.Files.Count
    33.                 ' if it is a .tbp file
    34.                 If Right$(LCase(Data.Files.Item(1)), 4) = ".tbp" Then
    35.                     ' enable dropping
    36.                     Effect = vbDropEffectCopy
    37.                     ' and exit the sub, since the information has been gathered
    38.                     Exit For
    39.                 End If
    40.             Next i
    41.         End If
    42.     End If
    43.  
    44. Exit Sub
    45. ErrHand:
    46.     Debug.Print "Error " & Err.Number & ": " & Err.Description
    47.     Effect = vbDropEffectNone
    48.     Err.Clear
    49.    
    50. End Sub

    HTHY
    Last edited by wpearsall; Jul 10th, 2003 at 11:56 PM.
    Wayne

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    thankls for the wpearsall. I was just being stupid for not realising there was a count method for dataobject

    Em i know this is another question but believe me it'll be the last. The program will be well and truely finished by then. I've decided to use too methods to get file names into the program the drag and drop and the clipboard (thanks henrikf). I'd like to give the user the option of which method to use. Can any one tell me how i can use the menu editor to make to enties that have tick's beside them that toggle when u click on them

  10. #10
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    Code:
    WITH MenuItemName
        .checked = not .checked
    END WITH
    that should do it for you (If i understand correctly)

    just add it to the normal code of the menu
    Wayne

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    i know i said that the last wquestion would be the last question but i discovered a problem with the copy from clipboard code

    henrikf wrote this
    VB Code:
    1. num_file_names = DragQueryFile(drop_handle, -1, vbNullString, 0)

    however there was no declaration for dragqueryfile i did a search and found this

    VB Code:
    1. Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal hDrop As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long

    which im now using, however while that line is trying to get the number of files contained on the clipboard it dosent seem to. It only gives the first file in the list. I have no idea how to correct this as i cannot find an explanation for the dragqueryfile function. So does anyone know whats wrong?

  12. #12

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    i found out what was the problem it seems that windows dosent like vbnullstring but it dosent throw an error for some reason, instead nothing happens. Thanks for the help all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width