Solved

Browse for folder returns Mapped path not UNC path

Posted on 2004-11-02
502 Views
Last Modified: 2012-06-22
Im using the Browse for Folder  and File Open Save api to allow users to edit paths to documents and folders.  This of course returns the Mapped path instead of the UNC path.  However I need the UNC path because of inconsistent mapping of network drives among users.  Looking for some code to help make this happen as well as some ideas on how to implement this on a form level.

Thanks
0
Question by:BillPowell
    5 Comments
     
    LVL 19

    Expert Comment

    by:Rimvis
    Look at this link:

     Translating a Mapped Drive to a UNC Path
    http://vbnet.mvps.org/index.html?code/network/uncfrommappeddrive.htm

    It's for pure VB, but you should be able to translate it to VBA.
    0
     
    LVL 48

    Expert Comment

    by:Gustav Brock
    Here is the bare bone code to copy and paste into a new module:

    Option Compare Database
    Option Explicit

    Private Const UNIVERSAL_NAME_INFO_LEVEL As Long = &H1&

    Private Declare Function WNetGetUniversalName Lib "mpr.dll" Alias "WNetGetUniversalNameA" ( _
      ByVal lpLocalPath As String, _
      ByVal dwInfoLevel As Long, _
      ByRef lpBuffer As Any, _
      ByRef lpBufferSize As Long) _
      As Long

    Private Type UNIVERSAL_NAME_INFO
      lpUniversalName As String * &H400&
    End Type

    Public Function GetUniversalPath( _
      ByVal strDrivePath As String) _
      As String

    ' Get UNC path from remote drive letter or drive letter and file name.
    ' Empty string is returned for local or non-mapped drive letters.
    ' Remote path is *not* validated except for the drive letter.
    '
    ' Example:

    '   s:\docs\letter.txt
    '
    ' may return
    '
    '   \\FS1\SYS\DATA\docs\letter.txt
    '
    ' 2002-03-25. Cactus Data ApS, CPH.
     
      Dim typBuffer   As UNIVERSAL_NAME_INFO
      Dim lngBuffer   As Long
      Dim strPath     As String
     
      ' Obtain needed buffer size.
      Call WNetGetUniversalName(strDrivePath, UNIVERSAL_NAME_INFO_LEVEL, 0, lngBuffer)
      ' Retrieve UNC info.
      Call WNetGetUniversalName(strDrivePath, UNIVERSAL_NAME_INFO_LEVEL, typBuffer, lngBuffer)
     
      ' Extract UNC path from mixed buffer.
      strPath = typBuffer.lpUniversalName
      strPath = Mid$(strPath, InStr(strPath, vbNullChar) + 1)
      strPath = Left$(strPath, InStr(strPath, vbNullChar) - 1)

      GetUniversalPath = strPath

    End Function

    /gustav
    0
     
    LVL 11

    Author Comment

    by:BillPowell
    Ok, I will give this a try.  Its probably worth mentioning that sometimes the mapping will be to a local drive.  Does this pose any problems since there is no UNC path for your C drive?
    0
     
    LVL 34

    Assisted Solution

    by:flavo
    not sure if you gat ananswer, but i use this ( it reutrns c:\ for c:\ and d:\ for d:\ - for partitioned hdd)

    used like LetterToUNC("M:") and retruns \\sever\path\

    Option Compare Database
    Option Explicit
    '
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
               "SHBrowseForFolderA" (lpBrowseInfo As BrowseForFolderInfo _
                                    ) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
               "SHGetPathFromIDListA" (ByVal pidl As Long, _
                                       ByVal pszPath As String _
                                      ) As Long
                                     
    Private Const RESOURCETYPE_ANY = &H0
    Private Const RESOURCE_CONNECTED = &H1

          Private Type NETRESOURCE
             dwScope As Long
             dwType As Long
             dwDisplayType As Long
             dwUsage As Long
             lpLocalName As Long
             lpRemoteName As Long
             lpComment As Long
             lpProvider As Long
          End Type

          Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
             "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
             ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) _
             As Long

          Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
             "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
             lpBuffer As Any, lpBufferSize As Long) As Long

          Private Declare Function WNetCloseEnum Lib "mpr.dll" ( _
             ByVal hEnum As Long) As Long

          Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" _
             (ByVal lpString As Any) As Long

          Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
             (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long

    Private Type BrowseForFolderInfo
     hOwner         As Long
     pidlRoot       As Long
     pszDisplayName As String
     lpszTitle      As String
     ulFlags        As Long
     lpfn           As Long
     lParam         As Long
     iImage         As Long
    End Type



        Function LetterToUNC(DriveLetter As String) As String
             Dim hEnum As Long
             Dim NetInfo(1023) As NETRESOURCE
             Dim entries As Long
             Dim nStatus As Long
             Dim LocalName As String
             Dim UNCName As String
             Dim i As Long
             Dim r As Long


            If DriveLetter = "C:" Then
                LetterToUNC = "C:"
                Exit Function
            ElseIf DriveLetter = "D:" Then
                LetterToUNC = "D:"
                Exit Function
            End If
           

             ' Begin the enumeration
             nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _
                0&, ByVal 0&, hEnum)

             LetterToUNC = "Drive Letter Not Found"

             'Check for success from open enum
             If ((nStatus = 0) And (hEnum <> 0)) Then
                ' Set number of entries
                entries = 1024

                ' Enumerate the resource
                nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), _
                   CLng(Len(NetInfo(0))) * 1024)

                ' Check for success
                If nStatus = 0 Then
                   For i = 0 To entries - 1
                      ' Get the local name
                      LocalName = ""
                      If NetInfo(i).lpLocalName <> 0 Then
                         LocalName = Space(lstrlen(NetInfo(i).lpLocalName) + 1)
                         r = lstrcpy(LocalName, NetInfo(i).lpLocalName)
                      End If

                      ' Strip null character from end
                      If Len(LocalName) <> 0 Then
                         LocalName = Left(LocalName, (Len(LocalName) - 1))
                      End If

                      If UCase$(LocalName) = UCase$(DriveLetter) Then
                         ' Get the remote name
                         UNCName = ""
                         If NetInfo(i).lpRemoteName <> 0 Then
                            UNCName = Space(lstrlen(NetInfo(i).lpRemoteName) _
                               + 1)
                            r = lstrcpy(UNCName, NetInfo(i).lpRemoteName)
                         End If

                         ' Strip null character from end
                         If Len(UNCName) <> 0 Then
                            UNCName = Left(UNCName, (Len(UNCName) _
                               - 1))
                         End If

                         ' Return the UNC path to drive
                         LetterToUNC = UNCName

                         ' Exit the loop
                         Exit For
                      End If
                   Next i
                End If
             End If

             ' End enumeration
             nStatus = WNetCloseEnum(hEnum)
          End Function
    0
     
    LVL 48

    Accepted Solution

    by:
    > Its probably worth mentioning that sometimes the mapping will be to a local drive.  
    > Does this pose any problems since there is no UNC path for your C drive?

    No other problem than an UNC path for a non-shared local drive is empty.
    If you need a path returned anyway, modify the code to return the original path if no UNC path is fournd:

      ...
      strPath = Left$(strPath, InStr(strPath, vbNullChar) - 1)

      If Len(strPath) = 0 Then
        strPath = strDrivePath
      End If

      GetUniversalPath = strPath
      ...

    /gustav
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
    Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now