[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys]Private Const CONNECT_UPDATE_PROFILE = &H1[/FONT]
[FONT=Fixedsys]Private Const RESOURCE_CONNECTED As Long = &H1&
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCETYPE_DISK As Long = &H1&
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&[/FONT]
[FONT=Fixedsys]Private Declare Function WNetCancelConnection2 Lib "mpr.dll" _
Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long[/FONT]
[FONT=Fixedsys]Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2A" (lpNetResource As NETCONNECT, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Private Type NETCONNECT
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public Function MapDrive(LocalDrive As String, RemoteDrive As String, _
Optional Username As String, Optional Password As String) As Boolean[/FONT]
[FONT=Fixedsys][COLOR=green][/COLOR][/FONT]
[FONT=Fixedsys][COLOR=green]' Usage: MapDrive "Q:", "[/COLOR][/FONT][URL="file://\\RemoteMachine\RemoteDirectory"][FONT=Fixedsys][COLOR=green]\\RemoteMachine\RemoteDirectory[/COLOR][/FONT][/URL][FONT=Fixedsys][COLOR=green]", "MyLoginName", "MyPassword"[/COLOR][/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] Dim NetR As NETCONNECT[/FONT]
[FONT=Fixedsys] NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = Left(LocalDrive, 1) & ":"
NetR.lpRemoteName = RemoteDrive[/FONT]
[FONT=Fixedsys] MapDrive = (WNetAddConnection2(NetR, Username, Password, CONNECT_UPDATE_PROFILE) = 0)
End Function[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public Function UnMapDrive(LocalDrive As String) As Boolean[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys][COLOR=green]' Usage: UnMapDrive "Q:"[/COLOR][/FONT]
[FONT=Fixedsys][COLOR=#008000][/COLOR][/FONT]
[FONT=Fixedsys] Dim NetR As NETCONNECT[/FONT]
[FONT=Fixedsys] NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = Left(LocalDrive, 1) & ":"
NetR.lpRemoteName = ""[/FONT]
[FONT=Fixedsys] UnMapDrive = (WNetCancelConnection2(LocalDrive, CONNECT_UPDATE_PROFILE, False) = 0)[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]End Function[/FONT]