VBA & Macro Problems

How to Check if a SharePoint File Exists Using VBA

Elise WilliamsElise Williams Nov 4, 2026 870 views

Question details

The user needs to programmatically verify whether a specific file exists on a SharePoint Online site using a VBA macro.

How to Check if a SharePoint File Exists Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open SharePoint Online

Navigate to the target SharePoint document library using your web browser.

2
Sync the Document Library

Click the 'Sync' button in the toolbar to establish a connection with the OneDrive desktop client.

3
Locate Local Path

Open Windows File Explorer and find the newly synced folder, usually located under your organization's name.

4
Update VBA Code

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 a Synced Local Folder via OneDrive
Environment Variables: To make the macro work across different computers, use the VBA Environ("USERPROFILE") function to dynamically build the local C:\Users path.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and download the free installer for your operating system.
  2. 2. Install the Software: Run the setup file and follow the on-screen prompts to complete the installation.
  3. 3. Open Your Spreadsheets: Launch WPS Spreadsheets and effortlessly open your existing macro-enabled workbooks to continue your tasks.
Highly compatible with Microsoft Excel formats (.xlsx, .xls, .csv) and standard VBA macros.Lightweight application that opens large data files rapidly and reliably.Familiar user interface with a zero learning curve for seamless migration.Free to use with comprehensive and powerful spreadsheet capabilities.
microsoft office alternative - wps office

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.