This article was previously published under Q210221
Advanced: Requires expert coding, interoperability, and multiuser skills.
Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.
View products that this article applies to.
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">)
Keywords: KB210221, kbprogramming, kbhowto