Fix Corrupted Excel File Downloaded from SharePoint via VBA
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.

- 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 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).
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.
Determine the exact path to your file on the SharePoint server, for example: '/sites/YourSite/Shared Documents/YourFile.xlsx'.
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'.
Replace the URL in your VBA script's XMLHTTP or WinHttpRequest object with the newly constructed REST API URL and run the script again.

Implement Proper VBA Authentication
Pass the necessary authentication tokens in your VBA HTTP headers to prevent SharePoint from returning an Access Denied error.
Verify Local Destination Folder Permissions
Check that the destination folder on your computer is writable and not blocking the VBA save operation.
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. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
- 2. Install the Software: Run the downloaded installer and follow the simple on-screen instructions to set up WPS Office on your device.
- 3. Open Your Spreadsheets: Launch WPS Spreadsheets, click 'Open', and select your Excel files to start editing immediately with full format compatibility.

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.




