Você está na página 1de 2

Private Sub cmdMapDrive_Click()

Dim drive_letter As String


Dim share_name As String
Dim password As String

lblResult.Caption = "Working..."
Screen.MousePointer = vbHourglass
DoEvents

drive_letter = txtDriveLetter.Text
If InStr(drive_letter, ":") = 0 _
Then drive_letter = drive_letter & ":"
share_name = txtShareName.Text
password = txtPassword.Text

If WNetAddConnection(share_name, password, _
drive_letter) > 0 _
Then
lblResult.Caption = "Error mapping drive"
Else
lblResult.Caption = "Drive mapped"
End If

Screen.MousePointer = vbDefault
End Sub

****************************************************
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias
"WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String,
ByVal lpszLocalName As String) As Long
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
strLocalDriveLetter = "Z:" 'Local drive letter to be
mapped
strPassword = "" 'specify network password if
required
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive

If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter)


> 0 Then
MsgBox ("An Error occurred mapping the drive")
Else
MsgBox ("Drive successfully mapped!")
End If

******************************************************

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&
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
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
Public Function MapDrive(LocalDrive As String, _
RemoteDrive As String, Optional Username As String, _
Optional Password As String) As Boolean
'Example:
'MapDrive "Q:", "\\RemoteMachine\RemoteDirectory", _
'"MyLoginName", "MyPassword"
Dim NetR As NETCONNECT
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = Left$(LocalDrive, 1) & ":"
NetR.lpRemoteName = RemoteDrive
MapDrive = (WNetAddConnection2(NetR, Username, Password, _
CONNECT_UPDATE_PROFILE) = 0)

If MapDrive = True Then


lstScripts.AddItem "Drive " & LocalDrive & " mapped to " & RemoteDrive
Else
lstScripts.AddItem "Unable to Map " & LocalDrive & " to " & RemoteDrive
End If
End Function

Você também pode gostar