Article ID: 114943
Article Last Modified on 10/11/2006
'The following Declare Function should be entered on one single line.
Declare Function GetDriveType Lib "KERNEL" _
(ByVal nDrive As Integer) As Integer
'Declaration of Constants
Const DRIVE_INVALID = 0
' The function below returns an array of drive letters that can be
' assigned to a variant variable or used directly.
Function UsedDrives() As Variant
Dim DriveNum As Integer
Dim UsedDriveArray() As Variant
Dim CurrentDrive As Integer
Dim DriveCount As Integer
'Initializes UsedDriveArray to have zero elements
ReDim UsedDriveArray(0)
'Initializes counter variable
DriveCount = 0
'Loops through all possible drives (0 = A:, 1 = B:, etc.)
For DriveNum = 0 To 25
'Returns DRIVE_INVALID if no drive connected to drive letter
CurrentDrive = GetDriveType(DriveNum)
If Not CurrentDrive = DRIVE_INVALID Then
'Adds the drive letter to the array
UsedDriveArray(DriveCount) = Chr$(DriveNum + 65) + ":"
DriveCount = DriveCount + 1
'Allocates one more array element for current active drive
ReDim Preserve UsedDriveArray(UBound(UsedDriveArray) + 1)
End If
Next
ReDim Preserve UsedDriveArray(UBound(UsedDriveArray) - 1)
'Returns the array
UsedDrives = UsedDriveArray
End Function
'The following sub will display a message box for each active drive
Sub DriveTest()
Dim DriveArray As Variant
DriveArray = UsedDrives()
For x = LBound(DriveArray) To UBound(DriveArray)
MsgBox DriveArray(x)
Next
End Sub
Additional query words: 5.00c howto XL5
Keywords: kbhowto kbprogramming KB114943