Send Email from Excel with Office Scripts & Power Automate

Share

Facebook
Twitter
LinkedIn

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.

 

send email from Excel

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):

  1. You select a product and see the dynamic report
  2. Pick the person who will receive the report (from drop-down list)
  3. 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

  1. Go to Power Automate website and login with your credentials
  2. Create a new instant cloud flow
  3. Set the trigger as “When HTTP request is received”
  4. Add “Run Script” step in Excel
  5. Add Send an email (v2) step
  6. Set up the flow as depicted below.
Power Automate Flow for Email Sender

Obtain the Trigger URL

  1. Save your flow
  2. Now go back the “trigger” step (step 1 of your flow)
  3. Make sure you set the method to GET
  4. Expand and copy the URL.
URL for triggering the flow

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.
Adding a button - Office Script

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:

VBA Cons:

Office Script

Office Script Pros:

Office Script Cons:

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.

More automation with Excel

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

8 Responses to “Pivot Tables from large data-sets – 5 examples”

  1. Ron S says:

    Do you have links to any sites that can provide free, large, test data sets. Both large in diversity and large in total number of rows.

    • Chandoo says:

      Good question Ron. I suggest checking out kaggle.com, data.world or create your own with randbetween(). You can also get a complex business data-set from Microsoft Power BI website. It is contoso retail data.

  2. Steve J says:

    Hi Chandoo,
    I work with large data sets all the time (80-200MB files with 100Ks of rows and 20-40 columns) and I've taken a few steps to reduce the size (20-60MB) so they can better shared and work more quickly. These steps include: creating custom calculations in the pivot instead of having additional data columns, deleting the data tab and saving as an xlsb. I've even tried indexmatch instead of vlookup--although I'm not sure that saved much. Are there any other tricks to further reduce the file size? thanks, Steve

    • Chandoo says:

      Hi Steve,

      Good tips on how to reduce the file size and / or process time. Another thing I would definitely try is to use Data Model to load the data rather than keep it in the file. You would be,
      1. connect to source data file thru Power Query
      2. filter away any columns / rows that are not needed
      3. load the data to model
      4. make pivots from it

      This would reduce the file size while providing all the answers you need.

      Give it a try. See this video for some help - https://www.youtube.com/watch?v=5u7bpysO3FQ

  3. John Price says:

    Normally when Excel processes data it utilizes all four cores on a processor. Is it true that Excel reduces to only using two cores When calculating tables? Same issue if there were two cores present, it would reduce to one in a table?
    I ask because, I have personally noticed when i use tables the data is much slower than if I would have filtered it. I like tables for obvious reasons when working with datasets. Is this true.

    • Ron MVP says:

      John:
      I don't know if it is true that Excel Table processing only uses 2 threads/cores, but it is entirely possible. The program has to be enabled to handle multiple parallel threads. Excel Lists/Tables were added long ago, at a time when 2 processes was a reasonable upper limit. And, it could be that there simply is no way to program table processing to use more than 2 threads at a time...

  4. Jen says:

    When I've got a large data set, I will set my Excel priority to High thru Task Manager to allow it to use more available processing. Never use RealTime priority or you're completely locked up until Excel finishes.

Leave a Reply