Selected Answer
You can use this Function:
Function GetLocalPath(filePath As String) As String
Dim localBasePath As String
Dim localPath As String
' Define your OneDrive base folder path manually
localBasePath = "C:\Users\" & Environ("UserName") & "\OneDrive" ' Adjust this if necessary
' Check if the file path starts with "https://"
If Left(filePath, 8) = "https://" Then
' Extract the Tenant ID from the URL
Dim tenantID As String
tenantID = Mid(filePath, InStr(filePath, "live.net/") + 9, 16)
' Replace the base URL and structure with the local path
localPath = Replace(filePath, "https://d.docs.live.net/" & tenantID & "/", localBasePath & "\")
localPath = Replace(localPath, "/", "\") ' Convert forward slashes to backslashes
Else
' If not a URL, use the original file path
localPath = filePath
End If
' Explicitly replace encoded spaces (%20) with actual spaces
localPath = Replace(localPath, "%20", " ")
GetLocalPath = localPath
End Function
To use it in your macro try something like this.
Dim localPath as String
localFilePath = GetLocalPath(emailBodyFilePath)
I've just tried this in a recent project, but I have been using the previous function without issues for a few years
Function GetOneDrivePath() As String
GetOneDrivePath = Environ("OneDrive")
End Function
Sub TestOneDrive()
Debug.Print GetOneDrivePath
End Sub