Skip to Content
Technical Articles
Author's profile photo Diana Matache

SAP Intelligent RPA: Save Attachments from emails


This is a submission of the Enhance your bot building with templates blog post series.

In this post I will show you how to recursively save email attachments from selected emails. This tutorial is an extension of my previous tutorial, but you can use the code according with your requirement.

What you will learn in this tutorial: 

  • How to use the Outlook Library;
  • How to recursively save email attachments to a predefined location;


Microsoft Outlook

Desktop Studio

Steps to follow:

1.Create a new workflow;

2.Import Outlook Library Scripts;

3.Add activities in your workflow;

4.Enrich the generated code by your workflow with your custom logic;


1.Create a new workflow

Create a new project and give it a name.

Go to ‘Workflow’ perspective and create a new workflow.

2.Import Outlook Library Scripts

In the workspace of your new workflow you can use different functions for accessing and manipulating Microsoft Outlook files. In order for your project to compile and run without errors, you first have to enable the Outlook Library scripts in your project:

  • go to “Scripts” perspective;
  • select “Project” tab(bottom-left corner);
  • right click anywhere in the Panel;
  • select “Include library Script”: The “Add Library Script” window pops;
  • enable “Outlook integration”;
  • click on “Save”;

3.Add activities in your workflow

The first activity that you need to add in your workflow is a Custom activity so you can use the functions in the Outlook library. You can give a name and a description for this activity, so you can distinguish better its purpose. I will name mine “saveAttachments”. I used the same name for its corresponding step. Using suggestive names will also allow you to better organize your code.

The second activity you should include is an End scenario which indicates that your scenario has ended.

Your workflow should now look like this:

Important: Before you can continue with the rest of the functionalities you need to build your project. This way the script for the workflow will be generated and we can enhance it.

4.Enrich the generated code by your workflow with your custom logic

After you have built your project you can proceed to the Scripts perspective and open the script generated by your workflow ( it will have the same name ). Search for your custom activity and step by their name (here is where the suggestive name comes in handy). You can write the rest of the logic in this step.

For my example, I am using a list of emails generated by filtering with parameters in my outlook email account. For more details about how to create a filtering function for your emails, please refer to my other tutorial.

After I get the list of emails, along with their details, I am looping through each mail and:

  1. Write in the logs some details about the email (Subject, Date, Sender)
  2. Check if the email has attachments (noAttachments > 0). If there is more than one attachment, their names will be in an array format. I will loop through the attachments and download them separately, saving them with their names to my location.

I saved all my attachments in the log file of my project, but of course, you can choose another location. If you use a hardcoded value, make sure to double the slashes, as I did in my example:

path = ctx.options.path.log + “\\” + filename;

After each attachment is saved, a notification is sent to the console with the name of the downloaded file.

Below you can find my complete code (along with the search function) and some more detailed comments:

// ----------------------------------------------------------------
//   Step: saveAttachments
// ----------------------------------------------------------------
    saveAttachments: function(ev, sc, st) {
        var rootData =;
        ctx.workflow('saveAttachments', '5290af0c-8449-4755-8416-76779ebed4b3');
        // Save the attachments from the selected emails
        // Initializes “Microsoft Outlook” application.

        var mails = [];
        var i, j, path, filename, attachments, noAttachments;

        // Resets the working mails list.

        // Search the body of email for a keyword. Other criteria are also available.
            //fromEmail: "",
            //subject: "",
            //sender: "",
            textDescription: "%test%",
            //read: 0,
            //hasAttachment: 0,
            //date: {after : new Date("MM/JJ/AAAA"),before : new Date("MM/JJ/AAAA")},
            //maxRow: 10
            dontThrowExceptionIfNoMailFound: true

        // Get the list of mail information for the mails that fit the criteria.
        mails = ctx.outlook.mail.getFilteredTable();

        // Build the working mails list by retrieving each mail.

        if (mails.length) {
            for (i = 0; i < mails.length; i++) {
                    EntryID: mails[i]['EntryID'],
                    StoreID: mails[i]['StoreID']

            // Display some info about each email.
            for (i = 0; i < ctx.outlook.mail.getCollectionLength(); i++) {
                ctx.log("Mail no: " + i);
                ctx.log("From: " + mails[i]['Sender']);
                ctx.log("Subject: " + ctx.outlook.mail.getSubject(i));

                // Here we start the procedure of downloading the attachments in the email (if they exist).	

                // Get the number of attachments for each email.
                noAttachments = ctx.outlook.mail.getAttachmentsCount(i);
                // Get the name of the attachments.
                attachments = ctx.outlook.mail.getAttachmentsName(i);

                // 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('File downloaded: ' + filename);

        // Ends “Microsoft Outlook” application.
        sc.endStep(); // end Scenario


This blog post should help you to understand the use of the ‘Outlook Library’ and how to recursively save email attachments from selected emails. At the end, you should be able to understand these functionalities and use them in your scenarios.

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Vilas Salunke
      Vilas Salunke

      Hi Diana,

      I have successfully run this scenario, but in that it is saving all attachments including signature image How can I save only Pdf attachment?