How to Insert an Image from an API URL Using VBA in Excel
Question details
The user needs to insert an image into an Excel worksheet using a URL that points to an API endpoint instead of a direct image file.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Attempting to fetch and display an image from an API endpoint dynamically within an Excel spreadsheet.
- Observed behavior
- The standard IMAGE function or a basic VBA macro fails to render the image because the URL returns JSON metadata or requires parsing before the actual image bytes can be accessed.
Ensure you have the Developer tab enabled in Excel to access the VBA editor. Verify whether your API endpoint returns raw image bytes or a JSON response, as this determines the method you must use to fetch the image.
Use VBA to Parse JSON and Extract the Image URL
If the API returns JSON metadata, use a VBA macro to call the endpoint, parse the response to extract the direct image URL, and insert the image.
When an API endpoint returns JSON instead of an image file, the standard Excel IMAGE function cannot process it. You must write a VBA script to handle the HTTP request, parse the JSON payload to locate the actual image URL, and then download and insert the image object into your sheet.
Press Alt + F11 in Excel to launch the Visual Basic for Applications (VBA) editor.
Click on 'Insert' in the top menu and select 'Module' to create a blank workspace for your script.
Write a VBA script using the MSXML2.XMLHTTP object to send a GET request to your API endpoint.
Process the returned JSON string (using a JSON parsing library or string manipulation functions) to extract the direct URL ending in .jpg or .png.
Use the ActiveSheet.Pictures.Insert(actual_image_url) method within your macro to place the image into your specified cell.

Use the Built-in IMAGE Function
If the API endpoint is configured to return image bytes directly, you can use Excel's native IMAGE function without writing any VBA code.
Easily Handle Macros and API Data with WPS Spreadsheet
WPS Office offers robust VBA support and full compatibility with Microsoft Excel macros. You can easily write, edit, and execute scripts to fetch API data and insert images seamlessly into your spreadsheets.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the file where you want to insert images.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon.
- 3. Launch the VBA Editor: Click on 'VBA Editor' to write or paste your API fetching macro.
- 4. Run the Script: Execute the macro to automatically pull and parse images from your API into the spreadsheet.

Frequently Asked Questions
Why does the Excel IMAGE function return a #VALUE! error with my API link?
The IMAGE function only works when the provided URL resolves directly to a supported image file format (such as JPG, PNG, or BMP). If your API link returns JSON metadata or an HTML page, Excel cannot render it, resulting in an error.
How can I add authentication to my API request in VBA?
You can add authentication headers to your MSXML2.XMLHTTP request in VBA by using the setRequestHeader method before calling the send command. This allows you to pass API keys or bearer tokens required by the endpoint.
Does WPS Spreadsheet support Excel VBA macros for API calls?
Yes, WPS Office offers comprehensive support for VBA macros. You can run your existing Excel scripts to fetch API data, parse JSON, and insert images without needing to modify your code.




