VBA Tipp: Netzlaufwerk verbinden/trennen

Aus DBWiki
Wechseln zu: Navigation, Suche

Problem

Ich möchte per VBA Netzlaufwerke verbinden und wieder trennen. Es gibt 2 Möglichkeiten, dieses Problem zu lösen.

Lösung #1

Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
  (lpNetResource As NETRESOURCE, ByVal lpPassword As String, _
   ByVal lpUserName As String, ByVal dwFlags As Long) As Long
 
Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
  "WNetCancelConnection2A" (ByVal lpName As String, _
  ByVal dwFlags As Long, ByVal fForce As Long) As Long
 
Type NETRESOURCE
  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 Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
 
' Im Folgenden sind alle für den Bereich NETRESOURCE
' definierten Konstanten gelistet, nicht nur jene, die
' im Beispiel Verwendung finden.
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
' Konstanten:
 
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCELLED = 1223&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_DEVICE_IN_USE = 2404&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Public Const ERROR_NO_NETWORK = 1222&
Public Const ERROR_SESSION_CREDENTIAL_CONFLICT = 1219&
 
Public Function NetzlaufwerkVerbinden(strRemoteName As String, _
  Optional strLocalName As String = "", _
  Optional strUser As String = "", Optional strPass As String = "")
Dim NetR As NETRESOURCE, ErrInfo As Long
 
  With NetR
    .dwScope = RESOURCE_GLOBALNET
    .dwType = RESOURCETYPE_DISK
    .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    .dwUsage = RESOURCEUSAGE_CONNECTABLE
    .lpLocalName = strLocalName 'wenn leer, dann ohne Laufwerksbuchstaben verbinden
    .lpRemoteName = strRemoteName 'ein gültiger Freigabename
  '  .lpComment = "optional"
  '  .lpProvider =    ' Freilassen
  End With
  ' wenn strUser und strPass leer sind,
  ' werden die Informationen dem lokalen Kontext entnommen
  ErrInfo = WNetAddConnection2(NetR, strPass, strUser, CONNECT_UPDATE_PROFILE)
  If ErrInfo <> NO_ERROR Then _
    MsgBox "Fehler: " & ErrInfo & " Verbinden nicht erfolgreich!", _
      vbExclamation, "NetzlaufwerkVerbinden"
End Function
 
Public Function NetzlaufwerkTrennen(strLocalName As String)
Dim ErrInfo As Long
' es können entweder der Name des Netzlaufwerks
' oder auch sein UNC-Pfad angegeben werden
' strLocalName = "\\ServerName\ShareName"
' strLocalName = "X:"
 
  ErrInfo = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE, False)
  If ErrInfo <> NO_ERROR Then _
    MsgBox "Fehler: " & ErrInfo & " Trennen nicht erfolgreich!", _
      vbExclamation, "NetzlaufwerkTrennen"
End Function

Aufruf

Und dann z.B. verbinden mit:

NetzlaufwerkVerbinden "\\Antares\Kalk","K:","Kalkulation"

und trennen mit:

NetzlaufwerkTrennen "K:"

Lösung #2

Public Sub MapNetDrive(strDrive As String, strShare As String, _
strDomain As String, strUser As String, strPW As String)
 
'Anwendung:
'Call MapNetDrive("Laufwerksbuchstabe","\\Servername\Freigabename", _
                  "Domänenname","Username","Kennwort")
 
Call Shell("NET USE " & strDrive & ": " & strShare & _
" /USER:" & strDomain & "\" & strUser & " " & strPW)
 
End Sub
 
 
Public Sub UnMapNetDrive(strDrive As String)
 
'Anwendung:
'Call UnMapNetDrive("Laufwerksbuchstabe")
 
Call Shell("NET USE " & strDrive & ": /DELETE /Y")
 
End Sub

Aufruf

Und dann z.B. verbinden mit:

Call MapNetDrive("K",\\Antares\Kalk","MeineDomäne","Karl","KarlsGeheimesKennwort")

und trennen mit:

Call UnMapNetDrive("K")

Weblinks

Microsoft Knowledge Base Artikel Nr. Q173011: