VBA: Convert from local drive name to network drive name

10,223

After doing some research, I actually answered my own question. Here is the code for those who are interested. The following code gets the UNC path instead of the network share drive letter when the end users import their file:

Option Explicit

Private Declare Function SetCurrentDirectory _
Lib "kernel32" _
Alias "SetCurrentDirectoryA" ( _
ByVal lpPathName As String) _
As Long

Public Sub GetFilePath_Click()
Dim vFileToOpen As Variant
Dim strCurDir As String
Dim WikiName As String

'// Keep Original Dir
strCurDir = CurDir

'// Note: If the UNC path does not exist then
'// It will default to your current one
SetCurrentDirectory "\\network_name\"
vFileToOpen = Application.GetOpenFilename
If TypeName(vFileToOpen) <> "Boolean" Then
    Range("E6").Value = vFileToOpen
End If


'// End by resetting to last/original Dir
ChDir strCurDir

End Sub

The function below convert the file path that the imported file to HTML style.

Function Path2UNC(sFullName As String) As String
    ' Converts the mapped drive path in sFullName to a UNC path if one exists.
    ' If not, returns a null string

    Dim sDrive      As String
    Dim i           As Long
    Dim ModDrive1 As String

    Application.Volatile

    sDrive = UCase(Left(sFullName, 2))

    With CreateObject("WScript.Network").EnumNetworkDrives
        For i = 0 To .Count - 1 Step 2
            If .Item(i) = sDrive Then
                Path2UNC = .Item(i + 1) & Mid(sFullName, 3)
                Exit For
             End If
        Next
     End With

     ModDrive1 = Replace(Path2UNC, " ", "%20")
     Path2UNC = ModDrive1

 End Function
Share:
10,223
Futochan
Author by

Futochan

Updated on June 04, 2022

Comments

  • Futochan
    Futochan almost 2 years

    I have this macro that initially only used by me. But I need to distribute it to other people now. Basically, I wrote a macro that let you browse for file, and then it will convert my local path into network drive path (HTML style). As you can see from my code below, I am specifically referring to R drive and Z drive. However, if other people use it, they could have A drive and B drive instead. How do I rewrite the following such that, it will pull the network drive instead of local drive? Thanks!

    Private Sub GetFilePath_Click()
    
    FilePath = Application.GetOpenFilename()
    If FilePath <> False Then
        Range("E6").Value = FilePath
    End If
    
    End Sub
    

    A function that convert the file that selected into HTML path

    Function ModFilePath(FilePath As String) As String
    
    Dim HTMLFilePath As String
    Dim Drive1 As String
    Dim Drive2 As String
    Dim Drive3 As String
    
    On Error Resume Next
    
    HTMLFilePath = Replace(FilePath, " ", "%20")
    
    'I know somehow I need to rewrite this part
    Drive1 = Replace(HTMLFilePath, "R:\", "\\network_name\apple\")
    Drive2 = Replace(HTMLFilePath, "Z:\", "\\network_name\orange\")
    
    If Err.Number = 0 Then
        If Left(HTMLFilePath, 1) = "R" Then
            ModFilePath = Drive1
        Else
            If Left(HTMLFilePath, 1) = "Z" Then
                ModFilePath = Drive2
            End If
        End If
    
    Else
        ModFilePath = "Error"
    End If
    
    End Function