VBA & Macro Problems

How to Add JPG, JPEG, and PNG Images with Excel Office Scripts

John WilsonJohn Wilson Nov 11, 2026 869 views

Question details

The user needs to automate the insertion of image files (such as JPG, JPEG, and PNG) into an Excel workbook using Office Scripts, typically triggered via Power Automate.

How to Add JPG, JPEG, and PNG Images with Excel Office Scripts
Product
Excel
Device & OS
not provided
Scenario
Automating image insertion in cloud-based Excel workflows using Office Scripts and Power Automate.
Observed behavior
Images must be properly encoded as base64 strings and passed from Power Automate to the Excel Office Script's addImage method without incorrect prefixes or corrupted data.
Before you start

Ensure you have an active Microsoft 365 subscription with access to Power Automate and Excel on the web. Prepare the image files you wish to insert and verify they are standard formats like JPG, JPEG, or PNG.

Solution 1Recommended

Insert Images using Power Automate and Office Scripts

Use Power Automate to retrieve the image file, convert its binary content to a base64 string, and pass it to an Excel Office Script for insertion.

The addImage method in Excel Office Scripts handles JPG, JPEG, and PNG formats universally. The critical requirement is that the image data must be passed as a clean base64-encoded string.

When integrating this with Power Automate, it is essential to ensure the flow retrieves the actual binary file content and converts it correctly before sending it to the script. Passing just a filename or a file URL will result in an error or a broken image.

1
Create the Office Script in Excel

Open your workbook in Excel on the web, navigate to the 'Automate' tab, and create a new script. Define a parameter in the main function that accepts a base64 string, and use workbook.getActiveWorksheet().addImage(base64String) to insert it.

2
Fetch the Image in Power Automate

Create a new Power Automate flow. Add an action like 'Get file content' (from OneDrive, SharePoint, or another storage connector) to fetch the target JPG or PNG image.

3
Convert File Content to Base64

In your flow, use the base64() expression to convert the fetched file content into a base64 string format.

4
Run the Excel Office Script

Add the 'Run script' action for Excel Online (Business) to your flow. Select the script you created in step 1, and pass the generated base64 string as the required parameter.

Insert Images using Power Automate and Office Scripts
Base64 Data Prefix: Make sure your base64 string does not include the data URI scheme prefix (e.g., 'data:image/png;base64,'). The addImage method expects only the raw base64 encoded data to function properly.
Free Microsoft Office alternative

Try WPS Office for Powerful Spreadsheet Automation

If you are looking for a lightweight, cost-effective alternative to Microsoft Excel, WPS Office offers robust spreadsheet capabilities. It includes advanced JS Macros and VBA support for automating your daily tasks locally, without relying on complex cloud-based flows.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your spreadsheet. Navigate to the 'Tools' tab on the top ribbon.
  2. 2. Access the Macro Editor: Click on 'Macros' or 'JS Macro' to open the integrated development environment.
  3. 3. Write a Local Script: Write a simple script using the WPS API to insert a picture directly from a local file path, bypassing the need for base64 conversion.
  4. 4. Execute the Macro: Run the macro to instantly place the JPG or PNG image into your worksheet.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .csv).Built-in support for JS Macros and VBA to automate repetitive tasks locally.Lightweight installation with a familiar, easy-to-use interface.Free to use with powerful data analysis and visualization tools.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my image not displaying correctly when inserted via Office Scripts?

This usually happens if the base64 string is corrupted, incomplete, or includes an incorrect MIME type prefix. Ensure you are converting the actual binary file content and passing only the raw base64 encoded string.

Can I insert multiple images at once using an Office Script?

Yes, you can modify your Office Script to accept an array of base64 strings. You can then use a loop within the script to call the addImage method for each string, adjusting the placement coordinates to prevent them from overlapping.

How do I get the base64 string of an image in Power Automate?

You can use the base64() expression in your Power Automate flow. Simply pass the dynamic content output from a 'Get file content' action into this expression to generate the required base64 string.

Does the addImage method support image formats other than JPG and PNG?

The addImage method primarily supports standard web image formats like JPG, JPEG, PNG, and SVG. Other formats like TIFF or BMP may not render correctly and should be converted to a supported format before encoding to base64.