Wenn man im Windows Explorer eine oder mehrere Dateien auswählt und dann den Befehl "Kopieren" oder "Ausschneiden" wählt, werden alle ausgewählten Dateinamen in die Zwischenablage kopiert. VB selbst bietet jedoch keine Möglichkeit, genau diese in der Zwischenablage befindlichen Dateinamen zu ermitteln. Mit nachfolgender Funktion möchten wir für Abhilfe schaffen. Die Funktion prüft mittels des Windows API die Zwischenablage auf darin befindliche Dateinamen und gibt diese als String-Array zurück. Option Explicit ' benötigte API-Deklarationen Private Declare Function IsClipboardFormatAvailable Lib "user32" ( _ ByVal wFormat As Long) As Long Private Declare Function OpenClipboard Lib "user32" ( _ ByVal hwnd As Long) As Long Private Declare Function GetClipboardData Lib "user32" ( _ ByVal wFormat As Long) 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 CloseClipboard Lib "user32" () As Long Private Const CF_HDROP As Long = 15 Public Function GetClipboardFiles(ByRef Files() As String) As Long Dim nHandle As Long Dim nCount As Long Dim nLen As Long Dim sFile As String Dim nFormat As Long Dim hGlobal As Long Dim i As Long ' Prüfen, ob Dateien in der Zwischenablage vorhanden If IsClipboardFormatAvailable(CF_HDROP) > 0 Then ' Zwischenablage öffnen If OpenClipboard(0) <> 0 Then ' Handle holen nHandle = GetClipboardData(CF_HDROP) If nHandle <> 0 Then ' jetzt alle Dateinamen ermitteln nCount = DragQueryFile(nHandle, -1, vbNullString, 0) ReDim Files(nCount - 1) For i = 0 To nCount - 1 nLen = DragQueryFile(nHandle, i, vbNullString, 0) sFile = String$(nLen + 1, 0) nLen = DragQueryFile(nHandle, i, sFile, Len(sFile)) Files(i) = Left$(sFile, nLen) Next i End If ' Zwischenablage schließen CloseClipboard End If End If GetClipboardFiles = nCount End Function Aufrufbeispiel: Dim nCount As Long Dim sFiles() As String Dim i As Long List1.Clear nCount = GetClipboardFiles(sFiles()) If nCount > 0 Then For i = 0 To nCount - 1 List1.AddItem sFiles(i) Next i Else MsgBox "Zwischenablage enthält keine Dateien!", vbInformation End If |