Article ID: 138904
Article Last Modified on 1/19/2007
Option Explicit
Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long
Declare Function WNetAddConnection Lib "mpr.dll" Alias _
"WNetAddConnectionA" (ByVal lpszNetPath As String, _
ByVal lpszPassword As String, ByVal lpszLocalName _
As String) As Long
Public Function FreeDrive(MyShareName$, MyPWD$) As Integer
On Local Error GoTo FreeDrive_Err
Dim DriveNum As Long, FirstFreeDrive As String
Dim FirstDrive As Long, Results As Long
DriveNum = 2
Do
DriveNum = DriveNum + 1
FirstFreeDrive = Chr$(DriveNum + 65) + ":\"
FirstDrive = GetDriveType(FirstFreeDrive)
Loop Until FirstDrive = 1
FirstFreeDrive = Left(FirstFreeDrive, 2)
FreeDrive = WNetAddConnection(MyShareName$, MyPWD$, _
FirstFreeDrive)
FreeDrive_End:
Exit Function
FreeDrive_Err:
FreeDrive = Err
MsgBox Error$
Resume FreeDrive_End
End Function
?FreeDrive(<"\\servername\sharename">, <"MyPwd">)
90861 ACC: Connecting to the First Available Network Drive
Keywords: kbhowto kbprogramming KB138904