web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Excel to Email (Groupe...
Power Automate
Answered

Excel to Email (Grouped by email)

(0) ShareShare
ReportReport
Posted on by 71

I thought I had this one figured out but it still doesn't seem to be working 100%

 

rowan853_0-1675957676033.png

 

I am trying to use PAD so that it loops through the data in excel and sends one email with all matching records (rows) per unique email address.

 

The data set could be very large with 1000 - 3000 rows and the data is sorted by email.

 

Ideally when all matches for the email has been found (next row would not be a match due to it being sorted by email), stops and sends the email, then moves onto the next email in the list and repeats the process.

 

rowan853_2-1675960118029.png

 

 

I'm very new to PAD so any help would be appreciated.

 

 

 

 

System.TerminateProcess.TerminateProcessByName ProcessName: $'''excel'''
Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Users\\man\\Documents\\TMS.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance
Excel.GetFirstFreeColumnRow Instance: ExcelInstance FirstFreeColumn=> FirstFreeColumn FirstFreeRow=> FirstFreeRow
Excel.ReadFromExcel.ReadCells Instance: ExcelInstance StartColumn: 1 StartRow: 1 EndColumn: FirstFreeColumn - 1 EndRow: FirstFreeRow - 1 ReadAsText: True FirstLineIsHeader: True RangeValue=> ExcelData
Excel.ReadFromExcel.ReadCells Instance: ExcelInstance StartColumn: 1 StartRow: 1 EndColumn: $'''D''' EndRow: FirstFreeRow - 1 ReadAsText: True FirstLineIsHeader: True RangeValue=> ExcelData2
Variables.RetrieveDataTableColumnIntoList DataTable: ExcelData ColumnNameOrIndex: 4 ColumnAsList=> EmailList
Variables.RemoveDuplicateItemsFromList List: EmailList IgnoreCase: False
Excel.CloseExcel.Close Instance: ExcelInstance
SET MatchOne TO 0
SET MatchTwo TO 0
LOOP FOREACH Email IN EmailList
 SET TableBody TO $''' 
<html>
<style>
 

th, td {
 text-align: left;
 padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}

th {
 background-color: #04AA6D;
 color: white;
}

</style>
<body>

 

<table border =\"1\"><tr>
'''
 LOOP FOREACH CurrentColumn IN ExcelData2.Columns
 Text.AppendLine Text: TableBody LineToAppend: $'''<th>%CurrentColumn%</th>''' Result=> TableBody
 END
 Text.AppendLine Text: TableBody LineToAppend: $'''</tr>''' Result=> TableBody
 LOOP FOREACH CurrentItem IN ExcelData
 IF MatchOne = 0 THEN
 LOOP FOREACH CurrentColumn IN ExcelData2.Columns
 IF Email = CurrentItem[4] THEN
 Text.AppendLine Text: TableBody LineToAppend: $'''<td style=\"width:220px\">%CurrentItem[CurrentColumn]%</td>''' Result=> TableBody
 SET MatchTwo TO 1
 ELSE
 SET MatchOne TO MatchTwo
 END
 END
 Text.AppendLine Text: TableBody LineToAppend: $'''</tr>''' Result=> TableBody
 ELSE
 SET MatchOne TO 0
 SET MatchTwo TO 0
 Text.AppendLine Text: TableBody LineToAppend: $'''</html>''' Result=> TableBody
 Outlook.Launch Instance=> OutlookInstance
 Outlook.SendEmailThroughOutlook.SendEmailFromMailbox Instance: OutlookInstance Account: $'''send@statsgroup.com''' SendFrom: $'''send@email.com''' SendTo: $'''recieve@email.com''' Subject: $'''TMS Profile''' Body: $'''<p><span style=\"font-size:16px\">Hi,</span></p>
%CurrentItem[4]%<P>
%CurrentItem[5]%<p>

<p><span style=\"font-size:16px\">Here is a copy of your current Training Profile.</span></p>
 
<p>
%TableBody%
<p>
''' IsBodyHtml: True
 END
 END
END

 

 

 

  • Kaif_Siddique Profile Picture
    2,108 Super User 2024 Season 1 on at

    Hi @rowan853 ,

     

    I would suggest use Excel as Database and select records using SQL query.

     

    Here is a sample flow. Customize the query based on your condition. 

    Kaif_Siddique_2-1675972162832.png

    Connection String:

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=<<File Path>>;Extended Properties="Excel 12.0 Xml;HDR=YES";

     

    Execute SQL Statement:

    Kaif_Siddique_3-1675972306513.png

     

    Also, refer the below link to Read Excel File Data Using A SQL Connection:

    https://www.matthewdevaney.com/power-automate-desktop-read-filter-excel-with-sql-query/#:~:text=Insert%20a%20new%20Launch%20Excel,path%20stored%20in%20our%20variable.&text=Then%20add%20a%20Read%20from,available%20values%20from%20the%20worksheet.

     

    Hope this will help you.

     

    If I have answered your question, please mark my post as Solved.

    If you like my response, please give it a Thumbs Up.

     

    Regards

    Kaif

  • RR-27091314-0 Profile Picture
    71 on at

    Hi @Kaif_Siddique 

     

    Thanks for the suggestion however.


    Using SQL to filter would surely require me to have a separate script for each email address which wouldn’t be practical because:

     

    a) there could be over 200 unique email addresses (i.e. 200 unique where statement scripts)

    b) new email address could be added at any time which would require a new script each time

  • Verified answer
    Kaif_Siddique Profile Picture
    2,108 Super User 2024 Season 1 on at

    Hi @rowan853 ,

     

    This is practically possible.

     

    1. Open SQL Connection
    2. Execute the Statement
      • Select the distinct Email. Output will be stored in QueryResult
      • Query : Select distinct [Role in Company] from [Sheet1$];
    3. Now, use For each to get the each record from QueryResult.
      • Execute the Statement with where condition
      • Query : Select * from [Sheet1$] where [Role in Company] = '%CurrentItem[0]%';
    4. Close SQL Connection

    Sample Flow:

    Kaif_Siddique_0-1676054929513.png

     

    If I have answered your question, please mark my post as Solved.

    If you like my response, please give it a Thumbs Up.

     

    Regards

    Kaif

  • Verified answer
    werkbook Profile Picture
    36 on at

    @rowan853 

     

    The following solution will take an input Excel file, i.e. %ExcelFile%, and return a list of datatables, i.e. %ListOfDatatables%.

    How to filter and split a datatable into a list of datatables.png

     

    Each item in %ListOfDatatables% is derived by filtering the original datatable read from the Excel file. In this example, the datatable is filtered based on the column Email. The number of items in %ListOfDatatables% corresponds to the number of unique emails found in the original datatable.

     

    Note that you will need to enter the following SQL query in the action Execute SQL statement:

    Select * from [Sheet1$] where [Email] = '%CurrentEmail%';

     

    In the second For each loop, a subflow ConvertDatatableToHTMLTableAndSendEmail is called to convert the datatable into a HTML table which can then be included in the email body.

     

    PS: To read more about this topic, please visit https://werkbook.co/How-to-filter-and-split-a-datatable-into-a-list-of-datatables-a06d7fec9d834d10877b7cd92dc6ef0a

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
11manish Profile Picture

11manish 233 Super User 2026 Season 2

#2
David_MA Profile Picture

David_MA 227 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 178

Last 30 days Overall leaderboard