logo
search
VBA & Macro Problems

How to Insert an Image from an API URL Using VBA in Excel

Natalie TaylorNatalie Taylor Oct 14, 2026 872 views

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.

How to Insert an Image from an API URL Using VBA in Excel
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in Excel to launch the Visual Basic for Applications (VBA) editor.

2
Create a New Module

Click on 'Insert' in the top menu and select 'Module' to create a blank workspace for your script.

3
Write the HTTP Request

Write a VBA script using the MSXML2.XMLHTTP object to send a GET request to your API endpoint.

4
Parse the Response

Process the returned JSON string (using a JSON parsing library or string manipulation functions) to extract the direct URL ending in .jpg or .png.

5
Insert the Image

Use the ActiveSheet.Pictures.Insert(actual_image_url) method within your macro to place the image into your specified cell.

Use VBA to Parse JSON and Extract the Image URL
Enable Required References: You may need to enable 'Microsoft XML, v6.0' in the VBA Editor by navigating to Tools > References to make HTTP requests successfully.

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. 1. Open Your Workbook: Launch WPS Spreadsheet and open the file where you want to insert images.
  2. 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon.
  3. 3. Launch the VBA Editor: Click on 'VBA Editor' to write or paste your API fetching macro.
  4. 4. Run the Script: Execute the macro to automatically pull and parse images from your API into the spreadsheet.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) formats and VBA macros.Lightweight and fast, ensuring smooth execution of complex HTTP requests.Free to use with a familiar user interface, requiring zero learning curve.
microsoft office alternative - wps office

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.