How to get UNC path from Application.Path?

I want to get the path to the active workbook in vba code. ActiveWorkbook.Pathdoing this

BUT

I need this to get something like this:

\\MachineName\ShareFolder\ETC\ETC2

NOT

S:\ETC\ETC2

Where is S:displayed on \\MachineName\ShareFolder\. How should I do it?

+4
source share
3 answers
    Dim Drive As String
    Drive = Left(ActiveWorkbook.Path, 2)

    ActiveWorkbookPath = Replace(ActiveWorkbook.Path, Drive, GetNetworkPath(Drive))


Function GetNetworkPath(ByVal DriveName As String) As String

    Dim objNtWork  As Object
    Dim objDrives  As Object
    Dim lngLoop    As Long


    Set objNtWork = CreateObject("WScript.Network")
    Set objDrives = objNtWork.enumnetworkdrives

    For lngLoop = 0 To objDrives.Count - 1 Step 2
        If UCase(objDrives.Item(lngLoop)) = UCase(DriveName) Then
            GetNetworkPath = objDrives.Item(lngLoop + 1)
            Exit For
        End If
    Next

End Function
+1
source

Before trying to convert the path to a file or any of them, try to perform several other properties that the object offers Workbook. I personally use ActiveWorkbook.FullNamewith all my projects (which are located on network drives), and I never had a problem.

, , , , . (, , ), . , : https://pagecommunication.co.uk/2014/07/15/vba-to-convert-a-mapped-drive-letter-to-unc-path/ UNC . , Microsoft.Scripting.Runtime, .

+1

The code below will indicate the file path as requested.

Path = Right(CurDir(), Len(CurDir()) - InStr(CurDir(), "\") + 1)
0
source

Source: https://habr.com/ru/post/1691118/


All Articles