Runtime Error 1004: “The file could not be accessed” (Network drive timeouts).

Excel VBA throws Runtime Error 1004: “The file could not be accessed” when a macro tries to open, save, or query a workbook stored on a local area network (LAN), virtual private network (VPN), or mapped network drive (such as Z:\), but the network link drops, lags, or times out during the input/output handshake.

Quick Answer

To resolve network drive timeouts, replace mapped drive letters (e.g., Z:\Data\) with full Universal Naming Convention (UNC) paths (e.g., \\ServerName\Share\Data\), implement an automated retry loop with error handling to ride out brief latency spikes, and check that VPN or SMB connection timeouts are not dropping during large file transfers.


Why Network Drives Time Out in VBA

When VBA executes commands like Workbooks.Open or Workbook.SaveAs against a local hard drive (like C:\), the operating system responds in single-digit milliseconds. Network shares behave differently. File read and write operations rely on Server Message Block (SMB) network protocols, router handshakes, and network interface cards.

' Risky on slow or fluctuating network connections:
Set wb = Workbooks.Open("Z:\Accounting\MonthlyLedger.xlsx")

If a router hops, a VPN re-authenticates, or server load delays the file-lock handshake for longer than Excel’s internal communication threshold, Excel aborts the call and throws Error 1004.

This error differs from general file-not-found issues: the file exists and the path syntax is valid, but the network transport layer failed to complete the request before the timeout limit.


Key Diagnostic Steps and Solutions

1. Convert Mapped Drive Letters to UNC Paths

Windows mapped drive letters (such as X:\ or Z:\) are user-specific and volatile. If Windows puts a mapped connection to sleep, or if the drive mapping disconnects during background processing, VBA fails to resolve the path.

  • Mapped Path (Fragile): Z:\Finance\Forecast.xlsx
  • UNC Path (Resilient): \\CorporateServer\Shared\Finance\Forecast.xlsx

UNC paths bypass the Windows drive mapping layer entirely, connecting directly to the server resource name.

' Preferred approach:
Dim networkPath As String
networkPath = "\\CorporateServer\Shared\Finance\Forecast.xlsx"
Set wb = Workbooks.Open(networkPath)

If your macro interacts with local files that are locked by ghost processes, compare your symptoms with Runtime Error 1004: “Cannot access the file ‘filename.xlsx'” (Pathing issues)..

2. Implement an Exponential Backoff Retry Loop

Because network latency spikes are often temporary, you should not let a one-second network blip crash a long-running batch job. Wrap the network call in a retry loop using Application.Wait:

Function OpenWithRetry(filePath As String, maxRetries As Integer) As Workbook
    Dim attempts As Integer
    Dim wb As Workbook

    attempts = 0
    Do While wb Is Nothing And attempts < maxRetries
        attempts = attempts + 1
        On Error Resume Next
        Set wb = Workbooks.Open(filePath, ReadOnly:=True)
        On Error GoTo 0

        If wb Is Nothing Then
            ' Wait 2 seconds before retrying
            Application.Wait (Now + TimeValue("0:00:02"))
        End If
    Loop

    Set OpenWithRetry = wb
End Function

3. Copy Locally for Heavy Data Processing

If your code needs to loop through hundreds of megabytes of source data, repeatedly querying the network drive will degrade stability.

  • Use VBA’s FileCopy statement or the Scripting.FileSystemObject to copy the network file to a local scratchpad folder (such as Environ("TEMP")).
  • Open and process the file from the fast local drive.
  • If updates are needed, write the completed file back to the network share in a single operation.

If you are dealing with cloud-backed SharePoint or OneDrive sync drives rather than local network shares, review Fixing VBA errors caused by “OneDrive” autosave and temp file paths..