I had some old code, that I brushed up. It now works for my:
- personal OneDrive
- company OneDrive
- shared (company) SharePoint
It takes care of the localised shared compound folder name of SharePoint, thus it should work for you as well.
' Return the full local filename of a workbook stored in a shared folder on OneDrive or in SharePoint.
'
' 2021-12-29. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function LocalFullName( _
ByVal Workbook As Excel.Workbook) _
As String
Const HKeyCurrentUser As Long = &H80000001
Const KeyPath As String = "Software\SyncEngines\Providers\OneDrive\"
Const TeamLibraryType As String = "teamsite"
Const SplitValue As String = " - "
Dim RegProv As Object
Dim SubKeys() As Variant
Dim Key As Variant
Dim SubKeyName As String
Dim FullName As String
Dim SubPath As String
Dim LibraryTypeValue As String
Dim CidValue As String
Dim MountPointValue As String
Dim UrlNamespaceValue As String
Dim SplitPoint As Integer
Dim SharedFolderName As String
Dim ChopLength As Integer
Set RegProv = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
' Get default full name.
FullName = Workbook.FullName
' Read OneDrive settings.
RegProv.EnumKey HKeyCurrentUser, KeyPath, SubKeys
' Loop the found keys to find the one holding this workbook.
For Each Key In SubKeys
SubKeyName = KeyPath & Key
RegProv.GetStringValue HKeyCurrentUser, SubKeyName, "UrlNamespace", UrlNamespaceValue
' If FullName contains UrlNameSpace, this is the key holding the local mountpoint of the current file.
If InStr(FullName, UrlNamespaceValue) > 0 Then
' Get the mount point for OneDrive
RegProv.GetStringValue HKeyCurrentUser, SubKeyName, "MountPoint", MountPointValue
' Check if this a shared SharePoint path.
RegProv.GetStringValue HKeyCurrentUser, SubKeyName, "LibraryType", LibraryTypeValue
If LibraryTypeValue = TeamLibraryType Then
' Shared SharePoint company folder.
SplitPoint = InStrRev(MountPointValue, SplitValue)
If SplitPoint > 0 Then
' Get second part of the shared subfolder name (Folder - Shared) of the mount point.
SharedFolderName = Mid(MountPointValue, SplitPoint + Len(SplitValue))
Else
' Should not happen.
End If
' Cut the sub folder from the full name.
SubPath = Mid(FullName, Len(UrlNamespaceValue & SharedFolderName) + 1)
Else
' Get the CID
RegProv.GetStringValue HKeyCurrentUser, SubKeyName, "CID", CidValue
' Find the length of the parent path of FullName.
ChopLength = Len(UrlNamespaceValue & CidValue)
' Adjust for Commercial or Consumer path of OneDrive.
If CidValue <> "" Then
ChopLength = ChopLength + 1
ElseIf Right(UrlNamespaceValue, 1) = "/" Then
ChopLength = ChopLength - 1
End If
' Cut off the parent path of FullName to have the namespace and the CID only.
SubPath = Right(FullName, Len(FullName) - ChopLength)
End If
' Replace forward slashes in SubPath with backslashes.
FullName = MountPointValue & Replace(SubPath, "/", "\")
Exit For
End If
Next
Set RegProv = Nothing
LocalFullName = FullName
End Function