Hi Luca,
Thanks for posting in the Microsoft community.
I understand you're encountering a "Not a valid file name" error when running your VBA function in Excel, and the file is stored on OneDrive.
This issue is often caused by how the file path is being interpreted by the VBA code, especially when working with cloud-based storage like OneDrive or SharePoint.
Here are two important steps to help you identify and resolve the issue:
1. Confirm the actual path being used by your VBA code
This step helps verify whether the file path is compatible with the OLEDB provider used in your VBA connection string.
In your code, you're using ThisWorkbook.Path to build the connection string. However, when the file is stored on OneDrive or SharePoint, ThisWorkbook.Path may return a web URL (e.g., https://agency-my.sharepoint.com/...) instead of a local file system path. This type of path is not supported by the Microsoft.ACE.OLEDB.12.0 provider.
To confirm this, add the following line to your code before opening the connection:
MsgBox "Workbook Path: " & ThisWorkbook.Path
If the message box shows a URL starting with https://, then the file is being accessed from the cloud and not from a local path. This is the reason for the "Not a valid file name" error.
2. Open the file from the local OneDrive sync folder
This ensures that ThisWorkbook.Path returns a valid local file system path that the OLEDB provider can recognize.
To resolve the issue, make sure you're opening the Excel file from your locally synced OneDrive folder. This is typically located at a path like:
C:\Users\
When you open the file from this location, ThisWorkbook.Path will return a proper local path (e.g., C:\Users\abc\OneDrive - Agency\Documents) that works with your connection string.
If you're unsure where your OneDrive files are syncing locally, you can:
- Right-click the OneDrive icon in the system tray.
- Select Settings > Account > Choose folders to see the sync location.
- Or open File Explorer and navigate to the OneDrive - [Your Organization] folder.
Once the file is opened from the local path, your VBA code should be able to connect without triggering the error.
Best wishes,
Peter | Microsoft Community Support