Skip to Content
Technical Articles

SAP Intelligent RPA – Write & Save Outlook Email Body into MS Excel

Introduction:

Microsoft Excel is most popular spreadsheet. Almost all private and government sectors use Microsoft Excel and It plays very important role to perform multiple activities on a daily basis. Coming to this blog post we will see below how to write outlook email body into Microsoft Excel and save the file. Please note that in this scenario the email is filtered by subject name.

Steps to Write & Save Outlook Email Body into Microsoft Excel:

  1. Create a new Microsoft Excel file.
  2. Create a new project in desktop studio.
  3. Select outlook and excel integration from library.
  4. Create a new work flow.
  5. Drag and drop, save and build to generate the code.
  6. Edit the code as shown below.
  7. Save, rebuild and debug.
  8. Finally, click on Bot to read and save outlook email body content into MS Excel.

Prerequisites:

  • Microsoft Outlook
  • Microsoft Excel
  • Desktop Studio 1.0.8.36

1. Create a new Microsoft Excel file:

Create a new Microsoft Excel file to save the outlook email body content into it.


2. Create a new project in desktop studio: 

  • Open the desktop studio > Click on File and click on new project.

 

  • Give the project name, title, client name and comments are optional and click on save.

3. Select outlook and excel integration from library:

  • Right click on script perspective and click on Include library script to select outlook and excel integration.

  • Select outlook and excel integration checkbox and click on save button.

4. Create a new work flow:

Right click on Global from work flow perspective and create a new work flow and give the name for work flow and click on save.

5. Drag and drop a custom activity:

From Activities we can drag and drop custom activity and rename it to “email body into excel” or whatever you want. Click on save button and build to generate the code.

6. Edit the code as shown below:

  • Once we click on build the below code will be generated. Now we can edit the code as per our requirement. Here the email is getting filtered by subject name as “Email Body into Excel through SAP Intelligent RPA”.
  • In desktop debugger mode we can read the content of outlook email body with this statement “ctx.log(“Email Body: ” + ctx.outlook.mail.getBody(0));”
  • Give the path of your created new Microsoft Excel file.
  • Also give the path to save the file.
// ----------------------------------------------------------------
//   Step: Email_Body_into_Excel
// ----------------------------------------------------------------
GLOBAL.step({ Email_Body_into_Excel: function(ev, sc, st) {
	var rootData = sc.data;
	ctx.workflow('EmailBodyToExcel', 'b416dab1-13d0-4e3c-8c10-f6890b82a5cd') ;
	// Email Body into Excel
	ctx.outlook.init();
	var emailExcel = [];
  var k = 0;
	ctx.outlook.mail.resetMailCollection();
	ctx.outlook.mail.searchByCriteria({
      subject: "Email Body into Excel through SAP Intelligent RPA",
      dontThrowExceptionIfNoMailFound: true
  });
	emailExcel = ctx.outlook.mail.getFilteredTable();
  if(emailExcel.length) {
		for(k=0; k<emailExcel.length; k++) {
      ctx.outlook.mail.retrieveMail({EntryID : emailExcel[k]['EntryID'],
			StoreID : emailExcel[k]['StoreID']});
    }
		//log
 		ctx.log("Email Body: " + ctx.outlook.mail.getBody(0));
  }
	//Give the path of your created excel file here
	  ctx.excel.file.open("C:\\ABC\\HO\\DEF\\PurchaseOrder.xlsx");
	  var writing = ctx.excel.sheet.setCell(2, 3, ctx.outlook.mail.getBody(0));
        //Give the path to save your file
	  var saving = ctx.excel.file.saveAs("C:\\ABC\\HO\\DEF\\PurchaseOrder.xlsx");
	 ctx.outlook.end();
	sc.endStep(); // end Scenario
	return;
}});


7. Save, rebuild and debug:

Save your project, click on rebuild if we are error free click on debug. We can read the outlook email body into our desktop debugger mode as shown below once you hover we get email body.

8. Click on Bot to read and save outlook email body content into MS Excel:

Once we debug our project successfully, from taskbar click on desktop agent and click on the bot to read outlook email and save it into Microsoft Excel file through SAP Intelligent RPA. We see now the below output.

 

Conclusion:

Indeed, the above blog post is simple and easy to do. This small post may help you to understand use of outlook and excel library and saving the outlook email body into Microsoft Excel file. With this we can proceed further to look into real time business requirements.

Enjoy SAP Intelligent RPA!

Thank you,

Syed Baba Tajuddin Hussaini

2 Comments
You must be Logged on to comment or reply to a post.
    • Hi Santosh,

      When we drag and drop the custom activity and click on build its related code will get generated. Entire code will not. As per our requirement we have to write the code manually. In above case we have written code from this line ctx.outlook.init(); to the last line.