|
-
Jul 8th, 2003, 04:25 PM
#1
Thread Starter
Member
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?
-
Jul 8th, 2003, 04:42 PM
#2
Hyperactive Member
use the clipboard object within vb ....
clipboard.getdata
-
Jul 8th, 2003, 04:45 PM
#3
Thread Starter
Member
I've tried that. All i get is a series of numbers that i have no idea what they mean.
-
Jul 9th, 2003, 03:10 PM
#4
Thread Starter
Member
-
Jul 9th, 2003, 04:27 PM
#5
Hyperactive Member
I actually meant clipboard.gettext, not clipboard.getdata...... but that dont work either. so i dunno :/
-
Jul 10th, 2003, 02:04 AM
#6
Junior Member
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
-
Jul 10th, 2003, 12:37 PM
#7
Thread Starter
Member
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
-
Jul 10th, 2003, 11:51 PM
#8
Frenzied Member
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
Last edited by wpearsall; Jul 10th, 2003 at 11:56 PM.
Wayne
-
Jul 11th, 2003, 12:25 PM
#9
Thread Starter
Member
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
-
Jul 11th, 2003, 07:15 PM
#10
Frenzied Member
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
-
Jul 12th, 2003, 08:31 AM
#11
Thread Starter
Member
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?
-
Jul 12th, 2003, 08:45 AM
#12
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|