When I view a file in Word online the URL displayed is: If workbookUri.(Once the hyperlink is created I will use it in OneNote to link to a file on my own Onedrive, for only me to use !) ' If workbook path points to local file, return it as-is Private Function GetWorkbookLocalPath(rawWorkbookUrl As String) As Stringĭim workbookUri = New Uri(rawWorkbookUrl) I have not yet tested this using all cases, but so far this appears to work. I used the great VBA solution proposed by as base and wrote a simplified, brute-force version in Vb.Net. If Not blnFileExist Then MsgBox "File could not be found in any OneDrive folders" 'display message if file could not be located in any OneDrive folders If objFSO.FileExist(strOneDriveLocalFilePath) ThenīlnFileExist = True 'that is it - WE GOT ITīlnFileExist = False 'not there try another OneDrive type (personal/business) If objFSO Is Nothing Then Set objFSO = CreateObject("Scripting.FileSystemObject") 'verify if file exist in this location and exit for loop if True StrOneDriveLocalFilePath = strOneDriveLocalPath & strFileEndPath 'this is the final file path on Local drive StrFileEndPath = Replace(strFileEndPath, "/", "\") 'flip slashes from URL type to File path type If Len(strFileEndPath) > 0 Then 'check if path found StrFileEndPath = Mid(strFileURL, i4thSlashPosition, Len(strFileURL) - i4thSlashPosition + 1) 'get the ending file path without pointer in OneDrive I4thSlashPosition = InStr(i4thSlashPosition + 1, strFileURL, "/") 'loop 4 times, looking for "/" after last found ' by replacing "https." with OneDrive local path obtained from registry we can get local file path 'for personal onedrive it looks like "" & file.FullName, _ StrFileEndPath = Mid(strFileURL, iDocumentsPosition, Len(strFileURL) - iDocumentsPosition + 1) 'get the ending file path without pointer in OneDrive IDocumentsPosition = InStr(1, strFileURL, "/Documents") + Len("/Documents") 'find "/Documents" position in file URL 'find "/Documents" in string and replace everything before the end with OneDrive local path 'for commercial OneDrive file path seems to be like "" & file.FullName) If strOneDriveLocalPath vbNullString Then StrOneDriveLocalPath = ShellScript.RegRead("HKEY_CURRENT_USER\Environment\" & strRegKeyName) Set ShellScript = CreateObject("WScript.Shell") On Error Resume Next 'invalid or non existin registry keys check would evaluate error Private Function strOneDriveLocalFilePath() As String
Simply call it from another sub or function to obtain the file path.
#Onedrive for business mac get link code#
The code checks all instances of OneDrive in the system and verify if file is there. In case your OneDrive folder is not in default location, the code below will allow you to locate the file wherever it is. StrRetVal = Replace(strRetVal, "/", "\") 'slashes in the right direction StrRetVal = RegKeyRead("HKEY_CURRENT_USER\Environment\OneDrive") & strRetVal
If Left(LCase(docPath), Len(strcOneDrivePart)) = strcOneDrivePart Then 'yep, it's the OneDrive path Private Function GetDocLocalPath(docPath As String) As String ' This is needed to get the local path, not the one drive path 'if the key cannot be found, the return value is ""įunction RegKeyRead(i_RegKey As String) As String 'reads the value for the registry key i_RegKey (Also notice I did change some slashes stuff for what I wanted returned! Other than that works great! Thank you so much! I was using excel not word, so the registry grabber "System." did not work for me. StrRetVal = Replace(strRetVal, "%20", " ") 'a space is a space once more StrRetVal = Replace(strRetVal, "/", "\") & "\" 'slashes in the right direction StrRetVal = System.PrivateProfileString("", "HKEY_CURRENT_USER\Environment", "OneDrive") & strRetVal 'read the "local part" from the registry and concatenate If Left(LCase(doc.Path), Len(strcOneDrivePart)) = strcOneDrivePart Then 'yep, it's the OneDrive pathīytSlashPos = InStr(Len(strcOneDrivePart) + 1, strRetVal, "/") 'return the local path for doc, which is either already a local document or a document on OneDriveĭim strRetVal As String, bytSlashPos As Byte Private Function GetDocLocalPath(doc As Document) As String
I'm using Word 2016 but I'm pretty sure this can be done in Word 2013 and Word 2010 as well. in which case I'll add my reply for anyone who's interested. It's been a while since you posted this so I suppose you don't need an answer any longer.