logo
search
File Corruption & Recovery

Fix Corrupted Excel File Downloaded from SharePoint via VBA

Olivia MillerOlivia Miller Sep 30, 2026 868 views

Question details

The user is attempting to download an Excel workbook from SharePoint using a VBA macro, but the downloaded file is small and corrupt, often containing an HTML error or login page instead of the actual data.

How to Fix a Corrupt Excel File Downloaded from SharePoint via VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Using a VBA script to automate the downloading of an Excel file hosted on a SharePoint site.
Observed behavior
The VBA code creates the file locally without crashing, but the resulting Excel file is corrupted, usually because the HTTP request returned an access-denied page or an HTML error page rather than the file bytes.
Before you start

Before modifying your VBA script, open the 'corrupted' downloaded file using a plain text editor like Notepad to verify if it contains HTML code (such as a 404 error or a Microsoft login page).

Solution 1Recommended

Update the SharePoint URL to the Correct REST Endpoint

Use the direct SharePoint REST API endpoint rather than a dynamic 'Copy Link' URL to ensure the script downloads the raw file data.

Dynamic web URLs generated by clicking 'Copy Link' in SharePoint often require browser cookies or interactive authentication, causing VBA to download the webpage instead of the file.

1
Identify the file's server-relative URL

Determine the exact path to your file on the SharePoint server, for example: '/sites/YourSite/Shared Documents/YourFile.xlsx'.

2
Construct the REST API URL

Format your URL to use the GetFileByServerRelativeUrl endpoint. It should look like this: 'https://[tenant].sharepoint.com/sites/[YourSite]/_api/web/GetFileByServerRelativeUrl('/sites/[YourSite]/Shared Documents/[YourFile.xlsx]')/$value'.

3
Update the VBA HTTP Request

Replace the URL in your VBA script's XMLHTTP or WinHttpRequest object with the newly constructed REST API URL and run the script again.

Update the SharePoint URL to the Correct REST Endpoint
URL Encoding: Ensure any spaces in your file path are properly URL-encoded (e.g., replacing spaces with %20) within the VBA script.
Free Microsoft Office alternative

Experience Seamless Spreadsheet Management with WPS Office

If you frequently encounter complex VBA and SharePoint integration issues with Microsoft Excel, consider trying WPS Office. It provides a lightweight, highly compatible alternative that makes managing, viewing, and editing your spreadsheets effortless.

  1. 1. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
  2. 2. Install the Software: Run the downloaded installer and follow the simple on-screen instructions to set up WPS Office on your device.
  3. 3. Open Your Spreadsheets: Launch WPS Spreadsheets, click 'Open', and select your Excel files to start editing immediately with full format compatibility.
Fully compatible with Microsoft Office formats, including .xlsx, .xls, and .csvLightweight installation that runs smoothly on older devices without lagFamiliar user interface requiring zero learning curve for Excel usersBuilt-in PDF editing and format conversion tools at no extra cost
microsoft office alternative - wps office

Frequently Asked Questions

Why is my downloaded Excel file only a few kilobytes in size?

A very small file size usually indicates that the VBA script downloaded an HTML error page, a login prompt, or an 'Access Denied' message instead of the actual Excel workbook data.

Can I use the 'Copy Link' URL from SharePoint in my VBA script?

No, the 'Copy Link' feature generates dynamic, user-specific web links that require a browser to resolve and authenticate. You must use the direct file path or the SharePoint REST API endpoint in your VBA script.

How do I properly save the downloaded file stream in VBA?

You should use the ADODB.Stream object in your VBA script to capture the responseBody of your XMLHTTP request and write it directly to your local file path using binary mode.

Why does my VBA script work locally but fail on my work computer?

Work environments often have strict firewall settings, mandatory proxies, or antivirus software that can block automated script-based HTTP requests to SharePoint servers, causing the download to fail silently.