Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Local filename

0

Hi

how to get the local filename when when opening a workbook located in Sharepoint or OneDrive or Teams folder. 
when a workbook is not à local drive, workbook.filename is such as https://...../yyyy.xlsx which cannot be used with FSO or Vba.Dir instead of C:\xxx/\....\yyyy.xlsx

thanks

stephane

Answer
Discuss

Answers

1

Finally found a simple solution...
I use CurDir function which returns the local path
stephane

Discuss

Discussion

Well done Stephane. Your Answer may be of use to other Forum users too- thanks!
John_Ru (rep: 6722) May 21, '25 at 10:32 am
Add to Discussion
0

Hi Stephane and welcome to the Forum.

This isn't something I need to do but I suggest you follow this recent YouTube video (not on this site)...

How To Handle OneDrive Paths in VBA – Access Local Files from Cloud URLs

Hope this helps- if so, please remember to mark this Answer as Selected.

Discuss

Discussion

Hi John
unfortunately, the suggested solution will not work as onedrivepath is unknown, depending on client configuration 
nevertheless, thanks again
stephane
Stefri (rep: 5) May 12, '25 at 11:48 am
Stephane- you say "onedrivepath is unknown, depending on client configuration" and that video uses:
Environ("OneDriveConsumer")
 
to get the drive from a personal machine but can you try (in VBA's Immediate window):
Debug.print  Environ("OneDriveCommercial")

on a business machine?
John_Ru (rep: 6722) May 12, '25 at 1:31 pm
Add to Discussion
0

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
Discuss

Discussion

Finally found a simple solution...
I use CurDir function which returns the local path
stephane
Stefri (rep: 5) May 21, '25 at 9:36 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login