I am looking for a way to determine what files have been selected from Windows Explorer.
For example, I would like to be able to select a range of files, right click, and from the Menu options, choose my application and fill a list box with the selected files.
Creating the context menu to launch my app is not a problem (by just making the appropriate "command" entry in the HKEY_CLASSES_ROOT registry key), but the problem is how do you know what is selected from Explorer?
If you do a drag and drop, you can just use the data object to iterate the files. With no drag though, how can can you get this info out the operating system?
I think this might involve some serious APIs so how about 300 points?
2. Copy and Paste the following into the Form1 code window.
3. Press F5 to run. A blank Form1 will appear on the screen. Open Windows Explorer and Copy some files to the clipboard. Click anywhere on Form1 and you will get a message as to how many Files are on the clipboard.. AND.. the IDE immediate window will be populated with the file names.
4. <smile>
<----- Code Begin ----->
Option Explicit
Private Const CF_HDROP = 15&
Private Declare Function CloseClipboard Lib "USER32" _
() As Long
Private 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
Private Declare Function GetClipboardData Lib "USER32" _
(ByVal wFormat As Long) _
As Long
Private Declare Function IsClipboardFormatAvailable
(ByVal wFormat As Long) As Long
Private Declare Function OpenClipboard Lib "USER32" _
(ByVal hWnd As Long) As Long
Private Sub Form_Click()
Dim lngFileCount As Long
Dim strFileNames() As String
Dim lngIndex As Long
If xGetClipboardFileList(strF
Then
MsgBox (lngFileCount & " Files are on the Clipboard")
For lngIndex = LBound(strFileNames) To UBound(strFileNames)
Debug.Print strFileNames(lngIndex)
Next lngIndex
Else
MsgBox ("No files are on the clipboard")
End If
End Sub
Public Function xGetClipboardFileList _
(ByRef strFileNames() As String, _
ByRef lngFileCount As Long) _
As Boolean
Dim strFileName As String
Dim lngIndex As Long
Dim lngNull As Long
Dim lngReturn As Long
' Is data available
lngReturn = IsClipboardFormatAvailable
If lngReturn = 0 _
Then
Exit Function
End If
' Open the clipboard:
lngReturn = OpenClipboard(0&)
If lngReturn <= 0 _
Then
Exit Function
End If
' Get handle to CF_HDROP if any:
lngReturn = GetClipboardData(CF_HDROP)
If lngReturn = 0 _
Then
GoTo Tag999
End If
lngFileCount = DragQueryFile(lngReturn, -1&, "", 0)
If lngFileCount <= 0 _
Then
GoTo Tag999
End If
' Allocate space for return and working variables.
ReDim strFileNames(1 To lngFileCount) As String
strFileName = String$(255, 0)
' Retrieve each filename in Dropped Filelist.
For lngIndex = 1 To lngFileCount
DragQueryFile lngReturn, lngIndex - 1, strFileName, Len(strFileName)
lngNull = InStr(strFileName, vbNullChar)
If (lngNull <> 0) Then
strFileNames(lngIndex) = Left$(strFileName, lngNull - 1)
Else
strFileNames(lngIndex) = strFileName
End If
Next lngIndex
xGetClipboardFileList = True
Tag999:
CloseClipboard
End Function
<----- Code End ----->