XL5: Visual Basic Procedure to Return Active Drive Letters

ID: Q114943

The information in this article applies to:

SUMMARY

In Microsoft Excel version 5.0, you can create a Visual Basic procedure that uses the Microsoft Windows API (application programming interface) Library to return the drive letters of active drives. Below is an example of Visual Basic code you can use to accomplishing this task.

Sample Visual Basic Procedure

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."

WARNING: The Declare Function statements listed in the following macro are very sensitive. If used incorrectly, these functions may cause a general protection (GP) fault in Windows or cause serious software problems.

'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.00 5.00c howto
Keywords          : kbprg kbhowto 
Version           : 5.00 5.00c
Platform          : WINDOWS

Last Reviewed: September 3, 1997