Setting Account Name when using Microsoft.Office.Interop.Outlook Namespace

Hi, my workflow is required to check if a list of Outlook folders exists in the local outlook email account. I have achieved this using the following assigns.

OutlookApp = new Microsoft.Office.Interop.Outlook.ApplicationClass
OutlookFolders = OutlookApp.Session.DefaultStore.GetRootFolder().Folders

I then call a Get Outlook Message Activity in a Try Catch, with the Folder Property as one of the folders from the list. If an Argument Exception: “The specified folder is not found” is thrown, I then create the folder using this assign:

objTemp = OutlookFolders.Add(strFolderName,Type.Missing)

But lets say I have 3 outlook accounts in my Outlook App, if i want to do this with another account instead of the default account, how do i go about doing this?

For example, lets say I have 3 accounts logged in for my Outlook, user1@outlook.com, user2@outlook.com and user3@outlook.com.

Is there a way i can pass the account address as a variable to change the account the workflow will check for folders in?

Hi @pmilanraajp

=> Create Variable: First, create a variable to hold the account address you want to work with. Let’s call it accountAddress.

=> Use the Specified Account: Replace your existing code with the following modified version:

OutlookApp = new Microsoft.Office.Interop.Outlook.ApplicationClass;
OutlookNamespace = OutlookApp.GetNamespace("MAPI");

// Here, use the 'accountAddress' variable to specify the desired account
OutlookNamespace.Logon("user1@outlook.com", "YourPassword", Missing.Value, Missing.Value);

OutlookFolders = OutlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Folders;

// Perform your folder checks and creation here

OutlookNamespace.Logoff();

In this code, replace "user1@outlook.com" with the accountAddress variable. This will allow you to dynamically choose which Outlook account’s folders you want to interact with based on the input you provide.

=> Loop Through Accounts (Optional): If you have multiple accounts and want to loop through them, you can create a list of account addresses and loop through them, applying the same set of operations to each account.

I’m getting the error “Expression does not produce a value.” for this line. Is this meant to be written in an Assign Activity?

@pmilanraajp

Yes.

Regards

OutlookFolders = OutlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Folders;

For this assign, I get the error: “Outlook is ambiguous, imported from the namespaces or types 'UiPath.Mail.Activities or UiPath.Mail”

Is the Outlook in Outlook.OlDefaultFolders.olFolderInbox supposed to be another object?