Skip to Content
Technical Articles
Author's profile photo Jerome GRONDIN

SAP Intelligent RPA 2.0 : Understand the Outlook library to read mails

Hello bot builders !

 

With SAP Intelligent RPA (and especially SAP Intelligent RPA 2.0), it is quite easy to build bots to automate your processes.  But when it comes to Outlook, things might become a little complicated if you don’t have a quite good understanding of how this library is designed and how it works.

This blog post (which is related to this one, and can be considered as a part of this serie) is here to help !

Unlike the previous version where you had to write the javascript code to create your bot (for all the Outlook usage, at least), SAP Intelligent RPA 2.0 comes with a bunch of activities that are ready to use. But first, let’s see how this library works.

Overview of the library

Let’s do a quick exercise. Imagine you’re at home and you need to do some homework. You enter the library and here is what you see :

First thing to do : pick the books you need.

Then you sit at your desk, with a stack of books in front of you:

What’s next ? You cannot read all of them at the same time so you need to pick one book from the stack to start your work.

Now, replace the word book with mail. Here is what we get :

  • Select the mails you need
  • You’ve got a stack of mails
  • Pick one mail to work with it

You get the idea. Outlook library works the same way : you will search for emails, and then you will work with what we call the Context Current Email (also known as CCE). Don’t forget this name, as it will be used later in this blog post.

Again, you cannot have multiple Context Current Emails, just like you cannot read multiple books at the same time : you read one book (even partially) then you pick another book and start reading.

 

Setup

First thing to do when you want to work with Outlook in an automation : add a dependency to Oulook library !

Open the settings of your project, and simply add the Outlook library (see below) :

Ok so now we can start working with Outlook. Using the library analogy, to be able to read some books, we must enter the library: that’s why we always use the activity Open Outlook instance before using any other Outlook activity.

And when we are done, we leave the library : we use the Release Outlook instance activity. So we get the below structure :

Ok and now ?

 

How to select the CCE ?

To select the mails, we use the Search Email activity : it’s the first activity in the category [irpa_outlook] Email so you cannot forget to use it !

Moreover, this activity is quite easy to use ! When you edit its properties, you have a popup where all you need to do is select what you need to search (and also in which folder, etc.) :

Note : searchCriteriasList is… a list ! So you can add as many criteria as you wish.

 

This Search Mail activity kind of returns a list of mail matching the criteria. It represents the stack of books.

Warning : the Search Mail activity does not exactly return a list that you can save in some variable. The result is stored internally and all the activities will work with this “hidden” result.

Wait… We have a list but we can only work with one item at a time ?

Yes ! And here is how we can do it.

 

Work with one email at a time

To work with these mails, we need to insert a loop in our automation. For each iteration, we make sure that the CCE exists (at the beginning of the loop) and we move to the next item in the list (at the end of the loop).

A picture is worth a thousand words :

Let’s detail this piece of automation :

  1. We enter the library
  2. We search for some mails, depending the criteria we set
  3. Using this loop we will be able to work with all the found mails
  4. We need to make sure the CCE exists, using the Is Context Current Email Not Exist activity
  5. We test the result of the previous instruction. If true, let’s get out from the loop
  6. Otherwise we go to the default branch
  7. Here we can do whatever we want with the CCE (get the subject, get the recipients, get the attachments, etc.)
  8. Once we’re done with this CCE, let’s take the next item in the list and loop
  9. We leave the library
In Step 7, do NOT use the Delete Email activities, because they change the Context collection. If you want to delete multiple emails, please have a look at the documentation.

 

There are similar activities which are available to check that the CCE exists. Make sure you’re using Is Context Current Email Not Exist in that case.

 

Note : all the activities you can use when the CCE exists are available under the categories emailItem and properties

 

And now ?

Now it’s up to you to build automations where you can work with tons of mails. For example you can get all the POs you received, and for each one of them, download the attached files and process the PO in S4 !

As you could see, there was no need to write a single line of code, and that’s why working with Outlook has never been so easy !

Note : you can also have a look here to learn the best practices about Oulook on our Help portal.

 

Find more information on SAP Intelligent RPA:

Exchange knowledge: SAP Community | Q&A | Blog

Learn more: Webinars | Help Portal | openSAP

Explore: Product Information | Successful Use Cases

Try SAP Intelligent RPA for Free: Trial Version | Pre-built Bots

Follow us on: LinkedInTwitter and YouTube

 

Assigned tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sreehari V Pillai
      Sreehari V Pillai

      Well written and very informative. Thanks for sharing

      sree

      Author's profile photo Steven De Baerdemaeker
      Steven De Baerdemaeker

      Does the search work, across the entire mailbox, or only in the main inbox folder (without searching sub folders)?

      Author's profile photo Jerome GRONDIN
      Jerome GRONDIN
      Blog Post Author

      If you do not specify the folder in the search criteria, the bot will search only in the main inbox folder. If you want to search in a given subfolder, you need to set the input with the name of the subfolder.

      Author's profile photo Steven De Baerdemaeker
      Steven De Baerdemaeker

      But sometimes you just don't know upfront which sub-folders exist.

      Microsoft provided in their Outlook interop dll a way to allow a search over the entire mailbox, why is it not implemented?

      variable.Search(searchCriteria, Outlook.OlSearchScope.olSearchScopeAllFolders);

      https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.olsearchscope?view=outlook-pia

       

      Author's profile photo Zoey Pinto
      Zoey Pinto

      This is really a helpful blog I am really impressed with your work, keep it up the good work

      SAP Intelligent RPA is a single unified Cloud-based solution. It includes “on-premise” automation tools as well. This is an end-to-end solution to automate steps. Most of us are familiar with “Macros and VBA Scripts” to automate certain tasks in Microsoft (MS) applications such as Excel.

      Author's profile photo Jerome GRONDIN
      Jerome GRONDIN
      Blog Post Author

      Thanks a lot ! Glad to help people better understand our tools 🙂

      Author's profile photo Jeorge Lumaban
      Jeorge Lumaban

      Hi Jerome,

      Using the search email, How to add search criteria for subject as element. I tried it for two criteria using 'And' 'OR' logic operator. See sample on the image but they get only one email.

      Author's profile photo Jerome GRONDIN
      Jerome GRONDIN
      Blog Post Author

      Hello,

       

      You cannot use OR operator in the search criteria. If you want to search emails containing "Analytics Department" or containing "Finance Department" in the subject, you need to use 2 differents Search mail activities.

      I would suggest to use a common automation, where the input would be the search criterion. That way you can call the nested automation twice, using 2 different criteria.

       

      Regards,

      J.