How to Check if a SharePoint File Exists Using VBA
Question details
The user needs to programmatically verify whether a specific file exists on a SharePoint Online site using a VBA macro.

- Product
- Excel VBA
- Device & OS
- Windows
- Scenario
- Attempting to use the VBA Dir function with an HTTPS SharePoint Online URL instead of a local mapped directory.
- Observed behavior
- The standard VBA Dir function fails or returns an error because it cannot parse HTTPS paths directly, as they are not mapped Windows folder paths.
Verify that you have adequate permissions to access the SharePoint document library and check if the OneDrive desktop sync app is installed on your computer.
Use a Synced Local Folder via OneDrive
Since the VBA Dir function natively reads local Windows paths, syncing your SharePoint library locally is the most reliable workaround.
By syncing your SharePoint library, files are mirrored on your local hard drive (usually under C:\Users). The VBA Dir command can easily check these mapped directories without encountering HTTPS authentication issues.
Navigate to the target SharePoint document library using your web browser.
Click the 'Sync' button in the toolbar to establish a connection with the OneDrive desktop client.
Open Windows File Explorer and find the newly synced folder, usually located under your organization's name.
Modify your VBA macro to use the new local path. Use a statement like: If Dir("C:\Users\[Username]\[Org]\[Library]\filename.xlsx") <> "" Then.

Use Microsoft XMLHTTP Request
If you cannot sync the folder locally, you can send an HTTP request via VBA to check the server's response code for the HTTPS URL.
Try WPS Office for Seamless Spreadsheet and Macro Handling
While checking SharePoint files via HTTPS URLs in VBA has limitations in Microsoft Office, you can simplify your workflow with WPS Office. WPS offers robust compatibility with Excel files, a familiar interface, and excellent local file management, making it an outstanding free alternative.
- 1. Download WPS Office: Visit the official WPS website and download the free installer for your operating system.
- 2. Install the Software: Run the setup file and follow the on-screen prompts to complete the installation.
- 3. Open Your Spreadsheets: Launch WPS Spreadsheets and effortlessly open your existing macro-enabled workbooks to continue your tasks.

Frequently Asked Questions
Why does the VBA Dir function fail with SharePoint URLs?
The built-in VBA Dir function is strictly designed to interact with standard Windows file system paths (like C:\ or network drives). It lacks the capability to parse, authenticate, or verify resources located at HTTPS web addresses.
Can I map a SharePoint drive to a network letter to fix this?
Yes, mapping a SharePoint document library to a network drive letter (e.g., Z:\) via Internet Explorer allows the Dir function to work. However, this older method is prone to connection timeouts and has largely been replaced by the more reliable OneDrive Sync client.
Does WPS Office support executing VBA macros?
Yes, specific versions of WPS Office include comprehensive VBA support, allowing you to run, edit, and troubleshoot macros originally created in Microsoft Excel. It handles local file system operations seamlessly.




