VBA Get File Path of Active Workbook as String without a URL

Temelan

New Member
Joined
Jul 13, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
When I use ThisWorkbook.path I get back a URL (https://...) instead of a file path (C:\...). How do I use VBA to get the local file path? For context, the workbook I'm using is saved in my OneDrive folder.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
So obviously open the workbook from the local drive rather than from OneDrive …​
 
Upvote 0
So obviously open the workbook from the local drive rather than from OneDrive …​
Ok... so I moved the workbook out of my OneDrive folder and ThisWorkbook.path functions correctly. Why is it that it doesn't work when the file is in the OneDrive folder? Does anyone know of a Sub that gets the local path of a workbook in the OneDrive folder?
 
Upvote 0
Try the function posted by MatChrupczalski on the msdn forum here > how to get physical path instead of URL (OneDrive)

Once you are on the webpage either search for his name and scroll up or search for his opening line which is:
"In case your OneDrive folder is not in default location, the code below will allow you to locate the file wherever it is."

I don't currently have access to Sharepoint but I think it might work for that too.
 
Upvote 1
If your file path is always the same, you could use something like this:

VBA Code:
Environ$("userprofile") & "\[OneDrive Folder Name]\Folder\Subfolder\etc.\"

This works for me. I have three OneDrives (2 for different employers and a personal one), so I need to identify the name of the OneDrive folder. I think you could use something like this if you only have a single OneDrive:

VBA Code:
Environ$("OneDrive") & "\Folder\Subfolder\etc.\"

Alternatively, you can turn off automatic syncing while running the macro, which should allow ThisWorkbook.Path to function normally.
 
Upvote 1

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top