cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jamesjson
Helper IV
Helper IV

Create a table using Excel fields and input it in an email

Hi,

 

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.

jamesjson_0-1645646333934.png

 

So for the example above, an email like below should get sent:

jamesjson_0-1645650389198.png

 

 

 

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.

 

 

Thanks!

 

13 REPLIES 13
UK_Mike
Post Prodigy
Post Prodigy

Perhaps use Excel to remove the duplicates in the first instance ?

Hi,

 

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. 

Yes but if you have 2 identical email addresses are the row values the same ?

No, the row values are different. 

 

So it should send ONE email with a table of the different values. Please see the picture below.

jamesjson_0-1645650454683.png

 

Ahh thats where you lost me, initially you said all of the rows value, as in single row but you meant table, gotcha.

Sorry about that. I see how that's confusing. I just updated it as plural

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 ?

MichaelAnnis
Super User
Super User

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%

End(If)

If %CurrentEmail% 'does not equal' %PreviousEmail%

    Capture the current row number as the %StartRow%

End(If)

If %CurrentEmail% 'does not equal' %NextEmail%

    Send Email with Excel extracted from A and %StartRow% to D and %LoopIndex% NOTE:  LoopIndex is the EndRow

End (if)

 

    

yoko2020
Responsive Resident
Responsive Resident

@jamesjson 

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

 

Requirements:

- 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: $'''your@email.com''' SendTo: $'''your@email.com''' Subject: $'''test''' Body: $'''%tableStart%
%tablehead%
%tableBody%
%tableEnd%''' IsBodyHtml: True
END

 

Hi,

 

I'm not understanding where you're getting the "Set %PreviousEmail% to %''%" 

 

Is %PreviousEmail% the %EmailAddress%

 

 

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!

Hi MichaelAnnis,

 

Thanks for your response! Are you also able to explain the first loop and how you're splitting between

 %CurrentEmail% and %NextEmail?

 

Thanks!

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%

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Top Solution Authors
Top Kudoed Authors
Users online (3,079)