How to Save a PNG Image from an HTTP Response using Excel VBA
Question details
The user needs a method to download and save a PNG image from an HTTP web server request using an Excel VBA macro.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the retrieval of binary image files (PNG) via an HTTP request and saving them directly to the active workbook's directory.
- Observed behavior
- The macro needs to fetch the file securely using ResponseBody instead of ResponseText to prevent the binary PNG data from getting corrupted during the download.
Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer tools and macros in your application settings.
Use XMLHTTP and ADODB.Stream to Save Binary PNG Data
This method uses the MSXML2.XMLHTTP object to send the network request and ADODB.Stream to correctly handle and save the binary image data to your local drive.
When handling images or other binary files in VBA, traditional text handling functions will corrupt the file. By combining an HTTP request's ResponseBody with an ADODB.Stream configured for binary data, the PNG file is downloaded and saved flawlessly.
In your active workbook, press Alt + F11 to launch the Visual Basic for Applications (VBA) Editor.
Click 'Insert' in the top menu bar and select 'Module' to create a new blank script window.
Create a subroutine that initializes an 'MSXML2.XMLHTTP' object. Use its '.Open' method with the 'GET' parameter and your target image URL, then call '.Send' to execute the request.
Check if the response '.Status' is 200. Create an 'ADODB.Stream' object, set its '.Type' to 1 (adTypeBinary), and call '.Open'. Write the '.ResponseBody' into the stream and use '.SaveToFile' to export it, utilizing 'ThisWorkbook.Path' to save it beside your XLSM file.

Execute Web Requests and VBA Macros in WPS Spreadsheet
WPS Spreadsheet provides outstanding compatibility with Microsoft Excel VBA macros. You can effortlessly write, edit, and run XMLHTTP and ADODB scripts to automate file downloads and streamline your workflow directly within WPS Office.
- 1. Enable Developer Tools: Open WPS Spreadsheet, navigate to 'Options', and ensure the 'Developer' tab is enabled to access the macro environment.
- 2. Open the Visual Basic Editor: Click the 'Developer' tab on the top ribbon and select 'Visual Basic Editor', or simply press Alt + F11.
- 3. Run Your Download Script: Paste your HTTP request and ADODB.Stream code into a standard module and hit 'Run' to seamlessly download the PNG file.

Frequently Asked Questions
Why is my downloaded PNG image unreadable or corrupted?
This commonly occurs if you use `.ResponseText` instead of `.ResponseBody` in your VBA script. PNGs are binary files, not text files. Standard text encoding corrupts the binary stream, so always extract the data using `.ResponseBody`.
How do I automatically save the image in the same folder as my workbook?
You can dynamically retrieve the active workbook's folder path by using `ThisWorkbook.Path`. Combine it with your desired file name like this: `ThisWorkbook.Path & "\downloaded_image.png"`.
Do I need to enable any specific VBA library references to run this macro?
If you are using 'early binding' (declaring variables as specific types), you need to enable 'Microsoft XML, v6.0' and 'Microsoft ActiveX Data Objects Library' via Tools > References. For 'late binding' (using `CreateObject`), no additional references are required.
Can I use this ResponseBody method to download other file types?
Yes. Because `.ResponseBody` captures raw binary data, you can use the exact same script logic to download any binary file type, including JPGs, PDFs, Excel files, or ZIP archives. Just ensure you specify the correct file extension when calling `.SaveToFile`.




