Here is a quick and dirty solution.
OLECHAR* OfficeAutomation::GetActiveDocPath()
{
VARIANT result;
VariantInit(&result);
m_hr = OLEMethod(DISPATCH_PROPERTYGET, &result, m_pActiveDocument, (LPOLESTR)L"Path", 0);
if (FAILED(m_hr))
return nullptr;
BSTR url = result.bstrVal;
if (wcsstr(url, L"https://") != nullptr)
{
wchar_t userName[UNLEN + 1];
DWORD userNameSize = UNLEN + 1;
GetUserNameW(userName, &userNameSize);
const wchar_t* rootFolder = L"https://d.docs.live.net/";
const size_t rootFolderLen = wcslen(rootFolder);
if (wcsstr(url, rootFolder) == url)
{
const wchar_t* slash = wcschr(url + rootFolderLen, L'/');
if (slash != nullptr)
{
const wchar_t* lastSlash = wcsrchr(slash, L'/');
if (lastSlash != nullptr)
{
const wchar_t* folderPath = slash + 1;
size_t folderPathLen = lastSlash - folderPath;
size_t localPathSize = MAX_PATH + wcslen(userName) + folderPathLen + 11;
wchar_t* localPath = new wchar_t[localPathSize];
wcscpy(localPath, L"C:\\Users\\");
wcscat(localPath, userName);
wcscat(localPath, L"\\OneDrive\\");
wcsncat(localPath, folderPath, folderPathLen);
wchar_t* backslash = wcschr(localPath, L'/');
while (backslash != nullptr)
{
*backslash = L'\\';
backslash = wcschr(backslash + 1, L'/');
}
BSTR resultPath = SysAllocString(localPath);
delete[] localPath;
return resultPath;
}
}
}
}
return url;
}