logo
search
SharePoint Document Issues

Fix VBA Dir Function Run-Time Error 52 for SharePoint Files

Maira MehtabMaira Mehtab Sep 20, 2026 868 views

Question details

The user needs to resolve a run-time error that occurs when a VBA script interacts with a file stored on SharePoint.

Product
Microsoft Visio Plan 2 / Office VBA
Device & OS
64-bit Windows 11
Scenario
Executing a VBA macro that uses Dir(ThisDocument.FullName) to query or manipulate a file hosted on SharePoint.
Observed behavior
The macro works properly for local and network files but throws run-time error 52 'Bad file name or number' when used with a SharePoint file.
Before you start

Verify whether the file path returned by ThisDocument.FullName is an HTTP/HTTPS URL, as the standard VBA Dir function does not natively support web paths.

Solution 1Recommended

Seek Code-Level Workarounds on Stack Overflow

Because this is a specific VBA coding limitation with SharePoint web paths, consulting the developer community for custom code solutions is highly recommended.

The VBA Dir function is historically designed to parse local drive letters and UNC network paths. When Office applications sync with SharePoint, ThisDocument.FullName often returns a web URL (HTTP/HTTPS) which triggers Error 52.

To resolve this, developers often write custom functions to map the SharePoint URL to the local OneDrive sync path. The developer community can provide exact code snippets tailored to your setup.

1
Gather your system and environment details

Note down your Office application version (e.g., Visio Plan 2), operating system (64-bit Windows 11), and the exact run-time error code (Error 52).

2
Prepare your VBA code snippet

Copy the failing VBA code, specifically the line containing Dir(ThisDocument.FullName), and include the exact string output of the SharePoint path format you are receiving.

3
Post your query to Stack Overflow

Navigate to Stack Overflow, create a new question, and apply the 'vba' and 'sharepoint' tags to attract VBA experts who can provide local-path conversion functions.

Free Microsoft Office alternative

Looking for a Seamless Office Alternative? Try WPS Office

While complex VBA integrations with SharePoint can sometimes cause compatibility headaches in Microsoft Office, WPS Office offers a lightweight, highly compatible alternative for your daily document needs. It handles standard Office formats flawlessly without the steep learning curve.

  1. 1. Download WPS Office: Visit the official WPS website and download the free version for your operating system.
  2. 2. Open your files: Launch WPS Office and open your existing Microsoft Office files directly with zero format loss.
  3. 3. Edit seamlessly: Enjoy smooth document creation and editing using an intuitive, familiar interface.
Fully compatible with Microsoft Word, Excel, and PowerPoint file formats.Lightweight design ensures fast installation and smooth performance.Familiar user interface requires no retraining to get started.Built-in cloud support for easy document sharing and backup.
QA img-10

Frequently Asked Questions

Why does the VBA Dir function return Error 52 on SharePoint files?

The VBA Dir function is natively designed to handle local file systems and standard network paths (UNC). It does not understand HTTP or HTTPS URLs, which are returned when a file is synced to SharePoint or OneDrive.

Can I use FileSystemObject (FSO) instead of Dir for SharePoint paths?

Like the Dir function, the standard FileSystemObject in VBA also expects local or UNC paths and will fail on web URLs. To work with SharePoint files, developers often map the SharePoint library to a local drive letter or use network location (WebDAV) paths.

How do I convert a SharePoint URL to a local path in VBA?

You generally need to write a custom VBA function that dynamically replaces the SharePoint URL prefix (e.g., https://company.sharepoint.com/) with the user's local OneDrive sync folder path by calling environment variables like Environ("OneDrive").