Tipp 1.24 - UNC-Dateipfad ermitteln

Wie kann ich einen UNC-Dateipfad ermitteln?

Mit dieser Funktion können Sie einen UNC-Dateipfad ermitteln. Kopieren Sie einfach nachfolgenden Quellcode in die Zwischenablage und fügen Sie anschließend den Inhalt der Zwischenablage in ein neues Modul ein. Die Aufrufparameter finden Sie im Quellcode beschrieben.

Api-AufrufeVerwendete Win32-Api-Aufrufe und Typen: WNetGetConnection

Beispiel:

Option Explicit
 
Public Declare Function WNetGetConnection _
    Lib "mpr.dll" Alias _
    "WNetGetConnectionA" _
    (ByVal lpszLocalName As String, _
    ByVal lpszRemoteName As String, _
    cbRemoteName As Long) _
    As Long
 
Public Function GetUNCPath( _
                ByVal sLocalPath As String) _
                As String
  '// -----------------------------------------------------
  '// Methode:   | Konvertiert einen Pfad in UNC-Pfad
  '//            | (\SERVER...)
  '// -----------------------------------------------------
  '// Parameter: | sLocalPath = gültiger, lokaler Pfad
  '//            |              (X:\..)
  '// -----------------------------------------------------
  '// Rückgabe:  | bei Erfolg = UNC-Pfad
  '//            | bei Fehler = sLocalPath
  '// -----------------------------------------------------
  '// Autor:     | Stefan Kulpa
  '//            | EDV Innovation & Consulting - Dormagen
  '// -----------------------------------------------------
 
  Const NO_ERROR  As Long = 0
  Dim sUNCPath    As String
  Dim sResult     As String
  Dim sDrive      As String
  GetUNCPath = sLocalPath
  If VBA.Mid$(sLocalPath, 2, 1) <> ":" Then Exit Function
  '// Die API-Funktion benötigt nur das Laufwerk!
  sDrive = VBA.Left$(sLocalPath, 2)
  sUNCPath = VBA.String(260, 0)
  If WNetGetConnection(sDrive, _
      sUNCPath, _
      VBA.Len(sUNCPath)) = NO_ERROR Then
    sResult = _
        VBA.Left$(sUNCPath, _
                  VBA.InStr(sUNCPath, vbNullChar) - 1)
    If VBA.Len(sResult) > 0 Then
      GetUNCPath = sResult & VBA.Mid$(sLocalPath, 3)
    End If
  End If
 
End Function