Microsoft Excel

Excel und VBA: Formeln, Programmierung, Lösungen

Die Excel/VBA-Beispiele

Thema

Erstes CD-ROM-Laufwerk ermitteln

Gruppe

System

Problem

Wie kann ich das erste CD-ROM-Laufwerk ermitteln?

Lösung
Geben Sie den nachfolgenden Code in ein Standardmodul ein und weisen Sie ihn einer Schaltfläche zu.

StandardModule: Modul1

Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
   (ByVal nDrive As String) As Long
Declare Function GetLogicalDriveStrings Lib "kernel32" Alias _
   "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, _
   ByVal lpBuffer As String) As Long
Public Const DRIVE_CDROM As Long = 5

Function GetFirstCdRomDriveLetter() As String
   Dim lDriveType As Long
   Dim strDrive As String
   Dim lStart As Long: lStart = 1
   Dim strDrives As String
   strDrives = Space(150)
   Dim lRetVal As Long
   lRetVal = GetLogicalDriveStrings(150, strDrives)
   If lRetVal = 0 Then
      GetFirstCdRomDriveLetter = vbNullString
      Exit Function
   End If
   strDrive = Mid(strDrives, lStart, 3)
   Do
      lDriveType = GetDriveType(strDrive)
      If lDriveType = DRIVE_CDROM Then
         GetFirstCdRomDriveLetter = strDrive
         Exit Function
      End If
      lStart = lStart + 4
      strDrive = Mid(strDrives, lStart, 3)
   Loop While (Mid(strDrives, lStart, 1) <> vbNullChar)
End Function

Sub Main()
   Dim sDriveLetter As String
   sDriveLetter = GetFirstCdRomDriveLetter()
   MsgBox sDriveLetter
End Sub