B
brotherescott
I am trying to write some VBA code to list all the drives on my
computer and if they are mapped show the full path they map to. I am
trying to do this to help work across many computers better. I am
trying to understand were some spreadsheet/macro users are getting some
data from.
Here is some code I got from the Microsoft Knowledge Base. It gives me
the list of logical dirve letters on my PC but no path for the mapped
drives.'
Thanks
Scott
Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Function GetDriveStrings() As String
' Wrapper for calling the GetLogicalDriveStrings API
Dim result As Long ' Result of our api calls
Dim strDrives As String ' String to pass to api call
Dim lenStrDrives As Long ' Length of the above string
' Call GetLogicalDriveStrings with a buffer size of zero to
' find out how large our stringbuffer needs to be
result = GetLogicalDriveStrings(0, strDrives)
strDrives = String(result, 0)
lenStrDrives = result
' Call again with our new buffer
result = GetLogicalDriveStrings(lenStrDrives, strDrives)
If result = 0 Then
' There was some error calling the API
' Pass back an empty string
' NOTE - TODO: Implement proper error handling here
GetDriveStrings = ""
Else
GetDriveStrings = strDrives
End If
End Function
Private Sub CommandButton1_Click()
Dim strDrives As String
' Find out what drives we have on this machine
strDrives = GetDriveStrings()
If strDrives = "" Then
' No drives were found
MsgBox "No Drives were found!", vbCritical
Else
' Walk through the string and list each drive
DisplayDriveTypes strDrives
End If
End Sub
Private Sub DisplayDriveTypes(drives As String)
' Walk through the logical drive string and display the drive
' letters. The logical drive string is a null seperated
' double null terminated string.
Dim pos As Long
Dim drive As String
ListBox1.Clear
pos = 1
Do While Not Mid$(drives, pos, 1) = Chr(0)
drive = Mid$(drives, pos, 3)
pos = pos + 4
ListBox1.AddItem UCase(drive)
Loop
End Sub
computer and if they are mapped show the full path they map to. I am
trying to do this to help work across many computers better. I am
trying to understand were some spreadsheet/macro users are getting some
data from.
Here is some code I got from the Microsoft Knowledge Base. It gives me
the list of logical dirve letters on my PC but no path for the mapped
drives.'
Thanks
Scott
Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Function GetDriveStrings() As String
' Wrapper for calling the GetLogicalDriveStrings API
Dim result As Long ' Result of our api calls
Dim strDrives As String ' String to pass to api call
Dim lenStrDrives As Long ' Length of the above string
' Call GetLogicalDriveStrings with a buffer size of zero to
' find out how large our stringbuffer needs to be
result = GetLogicalDriveStrings(0, strDrives)
strDrives = String(result, 0)
lenStrDrives = result
' Call again with our new buffer
result = GetLogicalDriveStrings(lenStrDrives, strDrives)
If result = 0 Then
' There was some error calling the API
' Pass back an empty string
' NOTE - TODO: Implement proper error handling here
GetDriveStrings = ""
Else
GetDriveStrings = strDrives
End If
End Function
Private Sub CommandButton1_Click()
Dim strDrives As String
' Find out what drives we have on this machine
strDrives = GetDriveStrings()
If strDrives = "" Then
' No drives were found
MsgBox "No Drives were found!", vbCritical
Else
' Walk through the string and list each drive
DisplayDriveTypes strDrives
End If
End Sub
Private Sub DisplayDriveTypes(drives As String)
' Walk through the logical drive string and display the drive
' letters. The logical drive string is a null seperated
' double null terminated string.
Dim pos As Long
Dim drive As String
ListBox1.Clear
pos = 1
Do While Not Mid$(drives, pos, 1) = Chr(0)
drive = Mid$(drives, pos, 3)
pos = pos + 4
ListBox1.AddItem UCase(drive)
Loop
End Sub