Combining a few earlier answers, I went with this function. It makes fewer assumptions about the format of a OneDrive URL and it uses the environment instead of the registry.
Note: it does still make assumptions about the OneDrive URL. Specifically:
- Assumed to start with "https:"
- Assumed only one URL path-component after the hostname
- Assumed that what follows will match the local file-system
Function GetWorkbookDirectory() As String
Dim sPath As String
Dim sOneDrive As String
Dim iPos As Integer
sPath = Application.ActiveWorkbook.Path
' Is this a OneDrive path?
If Left(sPath, 6) = "https:" Then
' Find the start of the "local part" of the name
iPos = InStr(sPath, "//") ' Find start of URL hostname
iPos = InStr(iPos + 2, sPath, "/") ' Find end of URL hostname
iPos = InStr(iPos + 1, sPath, "/") ' Find start of local part
' Join that with the local location for OneDrive files
sPath = Environ("OneDrive") & Mid(sPath, iPos)
sPath = Replace(sPath, "/", Application.PathSeparator)
End If
GetWorkbookDirectory = sPath
End Function