Anzeige
Archiv - Navigation
1520to1524
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

USB-Stick automatisch schließen

USB-Stick automatisch schließen
09.11.2016 16:55:35
Peter
Hallo Excel-Freunde,
ich möchte per Makro, nach erfolgter Datenübertragung, den USB-Stick automatisch schließen.
Ich habe darüber im Archiv nicht das Richtige finden können.
Kann mir Jemand behilflich sein ?
Danke im Voraus
Peter Malze

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: USB-Stick automatisch schließen
09.11.2016 18:50:43
Nepumuk
Hallo,
folgendes Beispiel wirft alle Wechseldatenträger aus:
Option Explicit

Private Declare PtrSafe Function CreateFileA Lib "kernel32.dll" ( _
    ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    ByRef lpSecurityAttributes As Any, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As LongPtr) As LongPtr
Private Declare PtrSafe Function DeviceIoControl Lib "kernel32.dll" ( _
    ByVal hDevice As LongPtr, _
    ByVal dwIoControlCode As Long, _
    ByRef lpInBuffer As Any, _
    ByVal nInBufferSize As Long, _
    ByRef lpOutBuffer As Any, _
    ByVal nOutBufferSize As Long, _
    ByRef lpBytesReturned As Long, _
    ByRef lpOverlapped As Any) As Long
Private Declare PtrSafe Function CloseHandle Lib "kernel32.dll" ( _
    ByVal hObject As LongPtr) As Long

Private Const DRIVETYPE_REMOVEABLE As Long = 1&
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const OPEN_EXISTING As Long = 3&
Private Const INVALID_HANDLE_VALUE As LongPtr = -1&
Private Const IOCTL_STORAGE_EJECT_MEDIA As Long = &H2D4808

Public Sub test2()
    Dim objFSO As Object, objDrives As Object, objDrive As Object
    Dim lngptrTempDrive As LongPtr
    Dim strDirveLetter As String
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objDrives = objFSO.Drives
    For Each objDrive In objDrives
        If objDrive.IsReady Then
            If objDrive.DriveType = DRIVETYPE_REMOVEABLE Then
                strDirveLetter = objDrive.DriveLetter & ":"
                lngptrTempDrive = CreateFileA("\\.\" & strDirveLetter, _
                    GENERIC_READ Or GENERIC_WRITE, 0&, ByVal 0&, OPEN_EXISTING, 0&, 0)
                If lngptrTempDrive <> INVALID_HANDLE_VALUE Then
                    Call DeviceIoControl(lngptrTempDrive, IOCTL_STORAGE_EJECT_MEDIA, _
                        ByVal 0&, 0&, ByVal 0&, 0&, 0&, ByVal 0&)
                    Call CloseHandle(lngptrTempDrive)
                End If
            End If
        End If
    Next
    Set objDrives = Nothing
    Set objFSO = Nothing
End Sub

Gruß
Nepumuk
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige