Do you have an Excel report or graphs that need to be emailed to various people every month? We can use Excel automation features to do this task quite easily.
The inspiration – A client request for Excel Email Solution
The idea for this came from a recent project I did for a client. They wanted me to build an Excel workbook which shows latest sales summary and then allows them to email the snapshot to the relevant people in one click.
Here is a snapshot of the solution I created for them (with dummy data):
- You select a product and see the dynamic report
- Pick the person who will receive the report (from drop-down list)
- Click on the “Send email” button to send the email
The ingredients – What we need
Here, I am using 3 main ingredients.
- A report created in Excel 365
- Office Scripts to generate the email contents and trigger the mail process
- Power Automate flow to send the email
Below is a schematic of the whole process.
The Recipe – How to send emails from Excel
The actual recipe is a bit detailed and harder to explain in text only format. So I made a video with the whole thing. Watch it here or on my YouTube channel. I have included the key steps as text below too.
Scripts & Instructions:
We can send either text or images as the email. In our case, I have both text content and images. The images come from a grouped object named Group 5.
GenerateReport Script:
Go to your “Automate” ribbon in Excel and click on “New Script” button.
In the script window, paste below script and customize the names as needed (refer to the video for explanation on the script).
function main(workbook: ExcelScript.Workbook):myOutput {
// Your code here
let ws = workbook.getWorksheet("Report");
let repGroup = ws.getShape("Group 5");
const repImage = repGroup.getImageAsBase64(ExcelScript.PictureFormat.png);
const emailSubject = ws.getRange("c2").getText();
const sendTo = ws.getRange("I22").getText();
console.log(emailSubject);
console.log(sendTo);
return {repImage,emailSubject,sendTo};
}
interface myOutput {
repImage: string;
emailSubject: string;
sendTo: string;
}
Set up the Power Automate Flow
- Go to Power Automate website and login with your credentials
- Create a new instant cloud flow
- Set the trigger as “When HTTP request is received”
- Add “Run Script” step in Excel
- Add Send an email (v2) step
- Set up the flow as depicted below.
Obtain the Trigger URL
- Save your flow
- Now go back the “trigger” step (step 1 of your flow)
- Make sure you set the method to GET
- Expand and copy the URL.
Back to Excel to make one more Script
We are nearly done. We just need to add one more script & a button in our sales report so that we can initiate the flow from Excel.
Add one more script in Excel and use the below code.
function main(workbook: ExcelScript.Workbook) {
// Your code here
const triggerURL = "___YOUR TRIGGER URL___";
let request = new XMLHttpRequest();
request.open("GET", triggerURL, false);
request.send(null);
}
- Save your script.
- From “Code Editor” click on the options menu for your script and use the “Add Button” to add a button on Excel worksheet.
- Whenever you click on this button, your flow will start.
Other ways to Automate this:
We can also use VBA to create & send emails automatically. I have previously written about that approach too. Read this article for VBA Excel Email Sender.
VBA vs. Office Script approach – which is better?
Both technologies offer automation. I have summarized the pros & cons of each technology below.
As of 2023 March, my preference is to use VBA for things like Email automation as it is easy to control and deploy.
VBA
VBA Pros:
- Works in any version of Excel
- Easy to learn, easy to code
- Lots of help & resources
- Very old and stable language base
- Works with Excel, Office & Windows Objects & API
VBA Cons:
- Can't use with Web / Mobile version of Excel
- Not easy to integrate with Cloud platforms (Power Automate, Sharepoint etc.)
- Security problems
Office Script
Office Script Pros:
- Works on Web / Mobile versions too
- Integrates with cloud platforms (Power Automate etc.)
- Future ready technology
Office Script Cons:
- No easy help or resources
- Hard language to learn and master
- Doesn't work in older versions of Excel
- Can't use all objects of Excel. Will not work with Windows API etc. too
- Needs costly subscription plans to use
- Runs on server, thus no control and susceptible to downtimes etc.
Thanks to Mark Proctor
I got the idea for URL trigger from Mark Proctor. Thanks Mark for the fantastic work 🙂
Got questions?
Do you have any questions reg. this implementation. Post a comment so that our community can help you.
One Response to “Send Email from Excel with Office Scripts & Power Automate”
This is exactly what I was looking for; thank you!
A couple things I found in my experience:
1. Power Automate has since been updated to a newer version, but the user still has the ability to toggle back to the original version (which changes the interface to look just like the one you used in the video).
2. I was able to incorporate this button on a shared workbook which is stored on a SharePoint site, but it did require obtaining a premium license for Power Automate (because of the HTTP trigger).
3. Instead of a chart, I used a variation of this solution to export a Named Range as an imbedded image within an email. As such, I had to slightly change some of the variables:
Instead of "ws.getShape()", I used "ws.getRange("[range name here]")". Of note: I'm not sure if MS removed it altogether in their last update, or if it was because I was using getRange, but it seemed that using getRange removed the "getImageAsBase64" function; getImage worked just fine instead (I think getImage now automatically defaults to base64 and the .png format).
May I offer a suggestion? In the script language, it may seem overly elementary to experts like you but some of us (me) are not trained programmers and it would be very helpful if we could tell which fields 'need to be' or 'can be' changed to fit our scenario (for example: "repGroup" doesn't have to be called repGroup - it can be called anything as long as the first letter is lowercase(?). Also, the name of the Worksheet should match whatever your worksheet is called - "Sheet1" for example).
Thank you SO much for providing your findings; it's helped me tremendously!