Skip to Content
Technical Articles
Author's profile photo Chaitanya Priya Puvvada

Challenge Submission:Explore Outlook functions, MS Word functions,creating a dynamic Email Content ,copying the dynamic content to PDF and looping activities.


This blog post explains the features which are used day in and day out when a BOT is developed and is a submission for the SAP Intelligent RPA Tutorial Challenge

1.Creating a Dynamic Email Content

Generating an automated email in one of the most common steps in executing any bot.

The content in the email needs to be changed according to the user. For example, for every mail the Email salutation the name changes. The name can be made Dynamic.

We generally write the HTML email body in the code and send the mail. There are scenarios where the Email content is too big. The better approach is to have the HTML email content in a text file and add special characters in the place where the content has to be changed Dynamically.

2.Printing data in PDF

There are business scenarios where we need to print the data in PDF. We don’t have the PDF activities in SDK. The solution is to print the data in MS word and save it as a PDF.


Since bot executes repetitive tasks, it would be a good approach if we use the Looping activities provided by SAP IRPA.

Workflow Sequence:

1. This bot reads the email content from the text files replaces the dynamic content.

2. Sends an email with the Dynamic Email Content.

3. Searches the unread mails.

4.Loops through the unread emails and prints the email content of all the mails in a word document.

5.Converts word document to a PDF.


Prerequisites :

Create a Folder in C drive C:\Users\Public\IRPA_projects\DynamicEmail\Data.(The path can be changed)

Create a text file Mailbody.txt.Copy all the HTML email content to a text file. Replace the dynamic content with Special characters as shown below.

Create a word document with the name Feedback.docx.This document will hold all the unread email content during the bot execution.

Include Word, Outlook libraries in your project.

Right-click on the Include script Library as shown below

Sequence of Steps

1. Create a project and the workflow with the sequence of the same steps.

Get Dynamic Email Body, Send Mail, Retrieve Unread Email, Set Email Index, Insert Word Content are Custom activities.

Create a context with the folder structure and items.

Set the properties for the Open Word activity. Enter the file name.

Set the exit condition for Exit Loop activity.

Enter the name of the PDF Document for the Save as Word Activity.

2. Step: Get Dynamic Email Body(reads the email content from the text files replaces the dynamic content.)

rootData.myData.sMailBody = mailBody.replace('####', 'Chaitanya Priya');
rootData.myData.sMailBody = rootData.myData.sMailBody.replace('$$$$', '4');


3. Send mail Step(Sends mail)

ctx.outlook.mail.create({ To: '', Subject: 'DynamicEmailContent' }); 
ctx.outlook.mail.setBodyHtml(0, rootData.myData.sMailBody);

4. Retrieve Unread Email Step(Retrieves the unread email from Outlook.I have set the maximum unread emails to 3){
    filter : "\"" + "urn:schemas:httpmail:read" + "\"" + "= 0", 
    maxRow : 3

5.Step to Set Mail Index to the length of the array of Unread emails .


6.Exit Loop Step (Exists the loop when the counter reaches the length of Unreadmails Array


7. Insert Word Content Step(Inserts the email content from the Unread email array and prints in the word document.

ctx.outlook.mail.retrieveMail({EntryID : rootData.myData.aUnreadMails[i]['EntryID'], StoreID : rootData.myData.aUnreadMails[i]['StoreID']});	rootData.myData.sWordContent = ctx.outlook.mail.getBody(i);
ctx.log("From: " + rootData.myData.aUnreadMails[i]['Sender']);       
 ctx.log("Subject: " + ctx.outlook.mail.getSubject(i)); 
 ctx.log("Body: " + ctx.outlook.mail.getBody(i)); 
 ctx.log("Body from root data: " + 	rootData.myData.sWordContent); 	var result = ctx.word.document.insertText(rootData.myData.sWordContent ); 
var result2 = ctx.word.document.insertParagraph( ); 


8. Save as Word Step(To save  the Word as PDF value 17 needed to be added in the code)



Now you should be able to utilize the MS word functions, Outlook functions, looping functions and create the dynamic email content.

Execution of BOT.

Folder structure after the bot execution is completed.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Anita Pareek
      Anita Pareek

      Very nicely written...good one 🙂

      Author's profile photo Vijay Sharma
      Vijay Sharma

      Nice .. thanks for sharing Chaitanya!!

      Author's profile photo Maximiliano Gonzales
      Maximiliano Gonzales

      Thank you very much Chaitanya  , I will try to replicate it.

      Author's profile photo Samyuktha Sasi Sekaran
      Samyuktha Sasi Sekaran

      Very Useful Blog Priya ,Thank you !

      Author's profile photo Ankita Gupta
      Ankita Gupta

      This is very nice blog Chaitanya. I will try to follow. Thanks!

      Author's profile photo Praveen Kumar Guduru
      Praveen Kumar Guduru

      Nice blog.

      Thanks for sharing.

      Author's profile photo Abhishek Malik
      Abhishek Malik

      Very informative blog Chaitanya!


      After reading your blog, I also tried making a bot for sending dynamic mail.

      But I am getting the following error :-.

      Can you please help me with it.

      Author's profile photo Chaitanya Priya Puvvada
      Chaitanya Priya Puvvada
      Blog Post Author

      Abhishek Malik

      Hi Abhishek,

      Can you confirm if you have enabled the outlook library for your project as mentioned in Pre-requisites.

      Did you initialize the outlook with the statement ctx.outlook.init().



      Author's profile photo Barry Neaves
      Barry Neaves


      unfortunately we have two different versions installed.  2010 and 2016. is there a way to call a specific version?  as it happens, the ‘ctx.outlook.init() command always calls 2010.  we are on citrix environments and we need to call the 2016 version.




      Author's profile photo Syed .
      Syed .

      Hi Chaitanya,

      Thanks a ton for writing this informative blog on SAP Intelligent RPA Outlook integration.

      Tried with the above example. I am unable to save the unread email content in word file and pdf file.

      These word and pdf files are empty. Cross checked with every step written above.

      Please guide what could be the reason for empty file. As per above I should get unread email content in word file and pdf file.

      Please guide in this regard at an earliest.

      Author's profile photo Chaitanya Priya Puvvada
      Chaitanya Priya Puvvada
      Blog Post Author

      Baba .

      As mentioned in step 7.

      Before checking the issue of empty file ,Can you check in debugging mode if you are successfully able to read the email content  from the Unread mails.

      Please check the value of the variable rootData.myData.sWordContent (step 7) by adding a log statement.


      Author's profile photo Syed .
      Syed .

      Hi Chaitanya,

      Thanks for your reply.

      You are correct. I am not able to read the email content from the unread email in step no 7 as you said.

      However, as per step no 4 above i.e Unread Email Step.Here I can read the email content in debugger mode by adding log statement.

      Request you to please guide further in this regard to save the data in word file.