1 1 1 1 1 1 1 1 1 1 Rating 0.00 (0 Votes)

Problemstellung:

Für 32Bit und 64Bit Office Versionen

Wie kann ich mit API ein Netzlaufwerk verbinden bzw. die Verbindung aufheben? 

1. Benötige API's und Konstanten:

' Netzlaufwerkfunktionen per Dialog

#If VBA7 Then
    'Code für 32 bit und 64 bit Office VBA 7
     #If Win64 Then
        'Code für 64-bit Office VBA 7
        Declare PtrSafe Function WNetConnectionDialog Lib "mpr.dll" _
            (ByVal hwnd As LongPtr, ByVal dwType As Long) As Long
        Declare PtrSafe Function WNetDisconnectDialog Lib "mpr.dll" _
            (ByVal hwnd As LongPtr, ByVal dwType As Long) As Long
        Declare PtrSafe Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" _
            (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
        Declare PtrSafe Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" _
            (ByVal lpszName As String, ByVal bForce As Long) As Long
     #Else
        'Code für 32-bit Office VBA 7
        Public Declare Function WNetConnectionDialog Lib "mpr.dll" ( _
            ByVal hWnd As Long, _
            ByVal dwType As Long) As Long
        Public Declare Function WNetDisconnectDialog Lib "mpr.dll" ( _
            ByVal hWnd As Long, _
            ByVal dwType As Long) As Long
        ' Netzlaufwerkfunktionen ohne Dialog
        Public Declare Function WNetAddConnection Lib "mpr.dll" _
            Alias "WNetAddConnectionA" ( _
            ByVal lpszNetPath As String, _
            ByVal lpszPassword As String, _
            ByVal lpszLocalName As String) As Long
        Public Declare Function WNetCancelConnection Lib "mpr.dll" _
            Alias "WNetCancelConnectionA" ( _
            ByVal lpszName As String, _
            ByVal bForce As Long) As Long
     #End If
#Else
    'Code für VBA 6 (32 bit)
    Public Declare Function WNetConnectionDialog Lib "mpr.dll" ( _
        ByVal hWnd As Long, _
        ByVal dwType As Long) As Long
    Public Declare Function WNetDisconnectDialog Lib "mpr.dll" ( _
        ByVal hWnd As Long, _
        ByVal dwType As Long) As Long
' Netzlaufwerkfunktionen ohne Dialog
    Public Declare Function WNetAddConnection Lib "mpr.dll" _
        Alias "WNetAddConnectionA" ( _
        ByVal lpszNetPath As String, _
        ByVal lpszPassword As String, _
        ByVal lpszLocalName As String) As Long
    Public Declare Function WNetCancelConnection Lib "mpr.dll" _
        Alias "WNetCancelConnectionA" ( _
        ByVal lpszName As String, _
        ByVal bForce As Long) As Long
#End If

Public Const RESOURCETYPE_DISK = &H1
Public Const WN_Success = &H0
Public Const WN_Net_Error = &H2
Public Const WN_Bad_Password = &H6
Public Const WN_Access_Denied = &H7
Public Const WN_Already_Connected = &H34
Public Const WN_Bad_NetName = &H32

Public Function API_DeleteFile(strFile As String) As Long
'*******************************************
'Name:      API_DeleteFile (Function)
'Purpose:   Datei löschen
'Author:    Tommyk
'Date:      März 11, 2004, 04:05:37
'Inputs:    strFile=Pfad und Name der Quelldatei
'Output:
'*******************************************
   API_DeleteFile = DeleteFile(strFile)
   If API_DeleteFile = 0 Then
      MsgBox "Löschen der Datei " & strFile & " Fehlgeschlagen", vbInformation, "Fehler"
      Exit Function
   Else
      MsgBox "Die Datei wurde erfolgreich gelöscht.", vbInformation, "Erfolgreich"
   End If
End Function
2. Verbinden per Dialog (im Formular):
Dim lResult As Long
lResult = WNetConnectionDialog(Me.hWnd, _
    RESOURCETYPE_DISK)

Ergebnis:


3. Verbindung per Dialog aufheben (im Formular):

Dim lResult As Long
lResult = WNetDisconnectDialog(Me.hWnd, _
    RESOURCETYPE_DISK)

Ergebnis:

 

4. Verbinden per Code:


Diese Funktionen unterstützen keine Übergabe des Usernamens und des Profils


Das wird ein extra Thema.

Public Function Connect_NetDrive(sDriveLetter As String, _
    sNetworkPath As String, _
    Optional sPWD As String = vbNullString) As Boolean
    Dim lResult As Long
    lResult = WNetAddConnection(sNetworkPath, _
        sPWD, sDriveLetter)
    If lResult <> WN_Success Then
        Select Case lResult
            Case WN_Net_Error
                MsgBox "Netzwerkfehler!"
            Case WN_Bad_Password
                MsgBox "Ungültiges Passwort!"
            Case WN_Access_Denied
                MsgBox "Zugriff verweigert"
            Case WN_Already_Connected
                MsgBox "Laufwerk ist bereits verbunden"
            Case WN_Bad_NetName
                MsgBox "Der Netzwerkpfad wurde nicht gefunden"
            Case Else
                MsgBox "Unbekannter Fehler"
        End Select
    End If
    Connect_NetDrive = (lResult = WN_Success)
End Function

Aufruf z.B.:
ohne Passwort:

Connect_NetDrive "E:", "\\PC-Name\Freigabename"

mit Passwort:

Connect_NetDrive "E:", \\PC-Name\Freigabename, "geheim"

 

5. Verbindung aufheben per Code:

Public Function Disconnect_NetDrive(sDriveLetter As String) As Boolean
    Dim lResult As Long
    lResult = WNetCancelConnection(sDriveLetter, True)
    If lResult <> WN_Success Then
        If lResult = WN_Net_Error Then
            MsgBox "Netzwerkfehler!"
        Else
            MsgBox "Unbekannter Fehler"
        End If
    End If
    Disconnect_NetDrive = (lResult = WN_Success)
End Function

Aufruf z.B.:

Disconnect_NetDrive "K:"
 

 

Ähnliche Artikel

You have no rights to post comments

Login Form

Neueste Artikel

SQL zu VBA Konverter
26. Oktober 2018
Problemstellung: Nur für 32Bit Office Versionen Gibt es eine Möglichkeit SQL-Code einer Abfrage so zu konvertieren das der Code in VBA genutzt werden kann? Lösung: Bis Access 2010 gibt das Tool...
1.png5.png7.png8.png6.png5.png1.png
Heute169
Gestern271
Diese Woche1000
Dieser Monat7274
Total1578651

  • IP: 52.55.55.239
  • Browser: Unknown
  • Version:
  • OS: Unknown

Online

2
Online

28. März 2024

Letzte Kommentare

  • Berechnen von Zeiträumen als Abfragekriterium

    elmard 02.02.2021 21:02
    1000 Dank
    für diese Datenbankanwendung! Eine sehr gute Umsetzung mit den vielen Möglichkeiten des Datums.

    Weiterlesen...

     
  • SQL zu VBA Konverter

    Tommy Admin 03.11.2019 16:33
    RE: SQL zu VBA Konverter
    Hallo Elmard, danke für die Info. :lol:

    Weiterlesen...

     
  • SQL zu VBA Konverter

    elmard 03.11.2019 14:49
    Bei SmartTools neue Version 4.0
    Dieses Tool liegt inzwischen in der Version 4 vor und läuft nun auch von A2013 und A2016 sowie im ...

    Weiterlesen...

     
  • Workshop zur Benutzung des Multi-Column TreeView Control unter MS-Access

    TommyK 27.02.2019 06:52
    Workshop
    Hallo mpegjunkie, danke für Dein Feedback. Schön das Dir Workshop weiter hilft. :D

    Weiterlesen...

     
  • Workshop zur Benutzung des Multi-Column TreeView Control unter MS-Access

    mpegjunkie 26.02.2019 20:10
    Perfekter Workshop
    Hallo Tommy, perfekter Workshop, toll und umfassend erläutert. Jetzt nutze ich diese Controls auch.

    Weiterlesen...