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?
Printable View
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?
use the clipboard object within vb ....
clipboard.getdata
I've tried that. All i get is a series of numbers that i have no idea what they mean.
^bump^
I actually meant clipboard.gettext, not clipboard.getdata...... but that dont work either. so i dunno :/
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
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:
Private Function GetFileList(Data As DataObject) On Error GoTo got_them_all Dim i As Integer Dim s As String i = 1 Do s = s & Data.Files(i) & "|" i = i + 1 Loop got_them_all: GetFileList = s 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
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:
Private Sub MDIForm_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, Y As Single) ' if there has been any file's dropped onto the form / control If Data.Files.Count > 0 Then Dim i As Long ' loop through the files which have been dropped onto the form / control ' to see if any of them are valid For i = 1 To Data.Files.Count ' if the file is not a .tbp file, dont do anything with it If ((GetAttr(Data.Files(i)) And vbDirectory) = vbDirectory) Or _ (Right$(LCase(Data.Files(i)), 4) <> ".tbp") Then ' directory / not .tbp file, don't bother with it Else ' it is a profile ext file, so try and load it, ' and if it fails to load, beep If LoadProfile(Data.Files(i), False) = False Then Beep End If Next i End If End Sub 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) On Error GoTo ErrHand: ' Don't allow dropping by default Effect = vbDropEffectNone ' if it is a list of files being dragged If Data.GetFormat(vbCFFiles) Then ' and there are some files being dragged onto the control / form If Data.Files.Count > 0 Then ' check to see if any of the files are a .tbp file Dim i As Long For i = 1 To Data.Files.Count ' if it is a .tbp file If Right$(LCase(Data.Files.Item(1)), 4) = ".tbp" Then ' enable dropping Effect = vbDropEffectCopy ' and exit the sub, since the information has been gathered Exit For End If Next i End If End If Exit Sub ErrHand: Debug.Print "Error " & Err.Number & ": " & Err.Description Effect = vbDropEffectNone Err.Clear End Sub
HTHY :)
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
that should do it for you (If i understand correctly) :)Code:
WITH MenuItemName
.checked = not .checked
END WITH
just add it to the normal code of the menu
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:
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:
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?
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