Option Explicit
Private Type POINTAPI
x As Long
Y As Long
End Type
Private Type GUID
lData1 As Long
iData2 As Integer
iData3 As Integer
aBData4(0 To 7) As Byte
End Type
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Type MSG
hwnd As LongPtr
message As Long
wParam As LongPtr
lParam As LongPtr
time As Long
pt As POINTAPI
End Type
Private Declare PtrSafe Sub DragAcceptFiles Lib "shell32.dll" (ByVal hwnd As LongPtr, ByVal fAccept As Long)
Private Declare PtrSafe Sub DragFinish Lib "shell32.dll" (ByVal HDROP As LongPtr)
Private Declare PtrSafe Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal HDROP As LongPtr, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long
Private Declare PtrSafe Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As LongPtr, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare PtrSafe Function WaitMessage Lib "user32" () As Long
Private Declare PtrSafe Sub AccessibleObjectFromWindow Lib "OLEACC.DLL" (ByVal hwnd As LongPtr, ByVal dwId As Long, riid As GUID, ppvObject As Any)
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
Private hwnd As LongPtr, HDROP As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Sub DragAcceptFiles Lib "shell32.dll" (ByVal hwnd As Long, ByVal fAccept As Long)
Private Declare Sub DragFinish Lib "shell32.dll" (ByVal HDROP 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 PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private Declare Sub AccessibleObjectFromWindow Lib "OLEACC.DLL" (ByVal hwnd As Long, ByVal dwId As Long, riid As GUID, ppvObject As Any)
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Private hwnd As Long, HDROP As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Private Const OBJID_NATIVEOM = &HFFFFFFF0
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Const PM_NOYIELD = &H2
Private Const PM_REMOVE = &H1
Private Const WM_DROPFILES = &H233
Private bXitLoop As Boolean
Private Const DROP_SHEET As String = "SHEET1" [B][COLOR=#008000]' <== change sheet name as required.[/COLOR][/B]
Private Const DROP_COLUMN As String = "A" [B][COLOR=#008000]' <== change column as required.[/COLOR][/B]
Public Sub StartDropping()
Dim FileDropMessage As MSG
Dim FileName As String * 256
Dim ret As Long
hwnd = GetWorkbookHwnd(ThisWorkbook)
DoEvents
DragAcceptFiles hwnd, True
DoEvents
bXitLoop = False
Do
WaitMessage
If PeekMessage(FileDropMessage, hwnd, WM_DROPFILES, WM_DROPFILES, PM_REMOVE Or PM_NOYIELD) Then
HDROP = FileDropMessage.wParam
ret = DragQueryFile(HDROP, 0, FileName, Len(FileName))
If ret Then
Call DropFileName(ThisWorkbook.Worksheets(DROP_SHEET).Columns(DROP_COLUMN), Left(FileName, ret))
End If
DragFinish HDROP
End If
DoEvents
Loop Until bXitLoop
End Sub
Public Sub StopDropping()
bXitLoop = True
End Sub
Private Sub DropFileName(ByVal DropColumn As Range, ByVal FileName As String)
Dim LastCell As Range
Set LastCell = Worksheets(DROP_SHEET).Cells(Columns(DROP_COLUMN).Rows.Count, 1).End(xlUp)
If IsEmpty(LastCell) Then LastCell = FileName Else LastCell.Offset(1) = FileName
End Sub
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Function GetWorkbookHwnd(ByVal wb As Workbook) As LongPtr
Dim hChild As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Function GetWorkbookHwnd(ByVal wb As Workbook) As Long
Dim hChild As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Dim ID_Dispatch As GUID
Dim oWindow As Window
With ID_Dispatch
.lData1 = &H20400
.iData2 = &H0
.iData3 = &H0
.aBData4(0) = &HC0
.aBData4(1) = &H0
.aBData4(2) = &H0
.aBData4(3) = &H0
.aBData4(4) = &H0
.aBData4(5) = &H0
.aBData4(6) = &H0
.aBData4(7) = &H46
End With
hChild = GetNextWindow(FindWindowEx(Application.hwnd, 0, "XLDESK", vbNullString), GW_CHILD)
Do While hChild
Call AccessibleObjectFromWindow(hChild, OBJID_NATIVEOM, ID_Dispatch, oWindow)
If oWindow.Parent Is wb Then GetWorkbookHwnd = hChild: Exit Function
hChild = GetNextWindow(hChild, GW_HWNDNEXT)
Loop
End Function