I am trying to grab specific fields from an Excel, create a table, and then input it in a body of an email.
For more background, I have an excel sheet where if an email address is listed, a notification will be sent out to that user. However, the email address can be listed more than once. I need to send out just ONE email with all of the rows' information with that email address.
So for the example above, an email like below should get sent:
I was able to create this using the web version, but due to some limitations, I have to use the desktop version.
Any help would be appreciated.
The duplicates aren't really the issue (I'm able to to that in the automation). Really, what I want is to pull the information from Excel and put it in the body of the email. However, 1 email per 1 email address listed in the Excel with ALL the information it has consolidated.
So its the whole table ? no other conditions ?
Like I said excel / power query can remove the duplicate email addresses and put them into a wee table same sheet or other sheet, for each email send table ?
If you have the freedom to sort by email, do so.
Here is the logic after launching excel and sorting:
Get First Free Row
Set %PreviousEmail% to %''%
Loop 2 (assuming a title row) to %FirstFreeRow - 1% increment of 1
'We will loop through each row with %LoopIndex% as the row number and grab the email from the current row and the next row in two separate variables into %CurrentEmail% and %NextEmail
If %LoopIndex% > 2
Read from Excel at D and %LoopIndex - 1%
If %CurrentEmail% 'does not equal' %PreviousEmail%
Capture the current row number as the %StartRow%
If %CurrentEmail% 'does not equal' %NextEmail%
Send Email with Excel extracted from A and %StartRow% to D and %LoopIndex% NOTE: LoopIndex is the EndRow
This is an example PAD code, you can copy and paste in PAD.
This is using sql query processing and outlook desktop for sending email
- Office365 64 bit installed.
- Outlook desktop configured
- Download sample excel file from here
PAD Code :
# ----------------CHANGE FILE PATH BELOW---------- SET FileExcel TO $'''C:\\Users\\abc\\Downloads\\Book1.xlsx''' SET tableStart TO $'''<div><table border=\"1\" cellpadding=\"2\">''' SET tablehead TO $'''<tr><th>Number</th><th>Name</th><th>Amount</th><th>Issues</th><th>Need to send email</th><th>Email Address</th></tr>''' Outlook.Launch Instance=> OutlookInstance SET ConnString TO $'''Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%FileExcel%;Extended Properties=\"Excel 12.0 Xml;HDR=yes\";''' Database.ExecuteSqlStatement.ConnectAndExecute ConnectionString: ConnString Statement: $'''Select DISTINCT [Email Address] from [Sheet1$] ''' Timeout: 30 Result=> QR_Email LOOP FOREACH CI_Email IN QR_Email SET tableBody TO $'''%''%''' SET EmailAddress TO CI_Email['Email Address'] Database.ExecuteSqlStatement.ConnectAndExecute ConnectionString: ConnString Statement: $'''Select * from [Sheet1$] where [Email Address] = \'%EmailAddress%\' ''' Timeout: 30 Result=> QR_Data LOOP FOREACH CI_Data IN QR_Data SET tableBody TO $'''%tableBody%<tr><td>%CI_Data['Number']%</td><td>%CI_Data['Amount']%</td><td>%CI_Data['Name']%</td><td>%CI_Data['Issues']%</td><td>%CI_Data['Need to send email']%</td><td>%CI_Data['Email Address']%</td></tr>''' END SET tableEnd TO $'''</table></div>''' Outlook.SendEmailThroughOutlook.SendEmail Instance: OutlookInstance Account: $'''email@example.com''' SendTo: $'''firstname.lastname@example.org''' Subject: $'''test''' Body: $'''%tableStart% %tablehead% %tableBody% %tableEnd%''' IsBodyHtml: True END
My apologies. I added another step (in BOLD) below. You are working off of two major logics.
Logic 1) If the Current Row Email is not the same as the Previous Row Email, get the Starting Row #
Logic 2) If the Next Row Email is not the same as the Current Row Email, send the Email (from Start Row to Current Row)
Set %PreviousEmail% to %''% is simply there so that the variable is defined as (blank) before the IF logic that happens later. This means your first row's email will not equal blank; therefore, it will declare the start row as 2 for the first email. Without this line of code, the IF function would error out because %PreviousEmail% would not have been populated yet.
Best of luck!
Thanks for your response! Are you also able to explain the first loop and how you're splitting between
%CurrentEmail% and %NextEmail?
Current Email would be:
Read from Excel at D and %LoopIndex% to %CurrentEmail%
Next Email would be:
Read from Excel at D and %LoopIndex + 1% to %NextEmail%
Keep up to date with current events and community announcements in the Power Automate community.
A great place where you can stay up to date with community calls and interact with the speakers.
Check out the latest Community Blog from the community!