Link to home
Start Free TrialLog in
Avatar of cplau
cplau

asked on

Mapping Network Drive

Hi All,

I would like map a network drive by connecting with different username & password.

I try this:

Private Const CONNECT_UPDATE_PROFILE = &H1

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

On Error GoTo Handler:

   Dim NetR As NETCONNECT

   NetR.dwScope = RESOURCE_GLOBALNET
   NetR.dwType = RESOURCETYPE_ANY
   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)

End Function


I found I cannot map a drive by providing username & password.

I can estabilish a connection by inputting the
local drive & remote drive only.

How can I solve this problem?
Thanks!



Avatar of aelatik
aelatik
Flag of Netherlands image

Try scripting ;

Dim WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
    WshNetwork.MapNetworkDrive "E:", "\\Server\Share", False, "username", "password"
Set WshNetwork = Nothing
Avatar of cplau
cplau

ASKER

Hi aelatik,

I've tried your code, but this error popup while I am tring to map
a network drive

Run-time error '-2147023677 (800704c3)'
The credentials supplied confilct with an existing set of credentials.

How can I solve this?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of daniellyh
daniellyh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cplau,
    Take a look at METHOD 2 on this page. Perhaps it will help.

http://www.vbusers.com/code/codeget.asp?ThreadID=248&PostID=1&NumReplies=0

Dang123