Skip to Content
Technical Articles

SAP Intelligent RPA – Filter Email Attachments

Introduction:

    • This is Syed Baba Tajuddin Hussaini with another blog post on SAP Intelligent RPA. It is being a long this blog post kept in draft. However, now publishing it on SAP Community.
    • Blog post is written with reference to @Diana Matache’s blog post i.e. Save Attachments from emails.
    • Email attachments are files with different formats, size and content.
    • As the heading of this blog post says we are going to have a look on the below steps to filter email attachments through SAP Intelligent RPA.
    • User receives multiple emails on daily basis containing different types of attachments like text.txt, word.docx, image.jpeg, excel.xlsx etc.
    • Here a user’s requirement is to filter Microsoft Excel Spreadsheet file irrespective of file name from email which contains multiple files with different formats in email.

Prerequisites:

  1. Microsoft Outlook.
  2. Desktop studio &
  3. Microsoft Excel Spreadsheet.

Step by step as follows:

1.  Create a new project:

  • Click on File > New project.
  • Give project name, title and client name.
  • Click on save.

2. 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.

3. Create a new workflow:

  • Right click on global to create a new workflow.
  • Give the workflow name and click on save
  •  In search bar search for custom activity
  • Drag and drop custom activity and rename it as Filter Email Attachments

 

4. Click on Build to generate the code:

  • Once we click on build following code will get generated.
  • Press enter on line number 40 and write the code as shown below.
// Filter Email Attachments
	ctx.fso.init();
	ctx.outlook.init();
	var emails =[]
	var i, j, path, filename, attachments, noAttachments, extract, spreadsheet;
	// Resets the working email list.
	ctx.outlook.mail.resetMailCollection();
	// Search email with subject name
	ctx.outlook.mail.searchByCriteria({
		subject: "Attachment",
		hasAttachment: true,
		dontThrowExceptionIfNoMailFound: true
	});
	// Get the list of email information for the emails that fit the criteria.
	emails = ctx.outlook.mail.getFilteredTable();
	if(emails.length){
		for (i = 0; i < emails.length; i++){
			ctx.outlook.mail.retrieveMail({
				EntryID: emails[i]['EntryID'],
				StoreID: emails[i]['StoreID']
			});
		}
		// Display some information about each email
		for(i = 0; i < ctx.outlook.mail.getCollectionLength(); i++){
			ctx.log("Email No:" + i);
			ctx.log("From:" + emails[i]['Sender']);
			ctx.log("Subject:" + ctx.outlook.mail.getSubject(i));
			// Get the number of attachments of each email.
			noAttachments = ctx.outlook.mail.getAttachmentsCount(i);
			// Get the name of attachments
			attachments = ctx.outlook.mail.getAttachmentsName(i);
			spreadsheet = ctx.fso.file.getExtensionName(filename);
			// Download the attachments if they exist.
			if (noAttachments > 0){
			// Save each attachment separately
				for (j = 0; j < noAttachments; j++){
					filename = attachments [j];
					path = ctx.options.path.log + "\\" + filename;
					ctx.outlook.mail.attachmentSave(i, path, {
						AttachmentName: filename
					});
					ctx.log("FileName:" + filename);
					var ExtensionName = ctx.fso.file.getExtensionName(ctx.options.path.log + "\\" + filename, true);
					var File = ctx.options.path.log + "\\" +filename;
					var FileName = ctx.fso.file.getFileName(File);
					if(ctx.options.path.log + "\\" + filename && "xlsx" === ExtensionName){
						ctx.excel.file.open(File);
						ctx.excel.file.saveAsPDF(0, ctx.options.path.log + "\\filename.pdf");
					}
					ctx.log("File Extension is:" + ExtensionName);
				}
			}
		}
	}
	 // Ends Microsoft Outlook application.
        ctx.outlook.end();
				ctx.fso.end();
	sc.endStep(); // end Scenario
	return;

 

5. Click on rebuild:

  • When we click on rebuild if we see zero error and zero warning. Click on Debug.

6. Click on Bot:

 

Conclusion:

We have seen in above steps how we can filter Outlook email attachments by file extension as xlsx. The bot search for an email with the given subject name and it downloads the multiple attachments and filter it for Microsoft Excel File and opens the same also it converts Microsoft Excel Spreadsheet to PDF as well.

 

Thank you,

Syed Baba Tajuddin Hussaini

Be the first to leave a comment
You must be Logged on to comment or reply to a post.