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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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