How to read Email from all Subfolders in Outlook / To get Path, Name of all Subfolders in Outlook

There are some Use Cases where we may have to read the mails from all the Subfolders Present in a Main folder of our Outlook Account.

To achieve this use the below VBA Code to get all the Subfolder with its full Path present in Inbox in your Outlook Account.

Dim olApp As Microsoft.Office.Interop.Outlook.Application
Dim olNs As Microsoft.Office.Interop.Outlook.Namespace
Dim olParentFolder As Microsoft.Office.Interop.Outlook.MAPIFolder
Dim olFolderA As Microsoft.Office.Interop.Outlook.MAPIFolder
Dim olFolderB As Microsoft.Office.Interop.Outlook.MAPIFolder
olApp = New Microsoft.Office.Interop.Outlook.Application
olNs = olApp.GetNamespace(“MAPI”)
olParentFolder = olNs.Folders(“YourEmail@outlook.com”).Folders(“Inbox”)
For Each olFolderA In olParentFolder.Folders
OutlookFolderNames.Add(olFolderA.FolderPath)
Next

Note : In the Place of YourEmail@outlook.com put your Email Id from which you wanted to retrieve mail Items.
Click on Edit Arguments and Pass a List(Of String) OutlookFolderNames Variable to hold the value of Subfolder Paths.

image

Now you have all the Subfolder Paths in a List Variable

  1. Use for each loop and loop over this List,
    In for each use get outlook mail message activity pass the loop item to MailFolder property of get outlook mail message activity
    You will get mail items from required subfolder, you can perform your business logic on it or you can add all the mail message to a List of mail messages variable and loop through the mail messages from the list and perform the required operation.

image

Happy Automation !!

Thanks & Regards,
Rohith

2 Likes

@rohith.prabhu . Thanks for the brilliant little code snippet to get Outlook folders. I needed to traverse all folders in a mailbox and do things with the emails. Because all the “For Each Email”, etc. activities are not friendly to the idea of actually knowing what folder an email lives in, I used your code to build a library that I can call and it calls itself recursively to build a list of all folders/subfolders and return it to the main program. The main program then uses that table to go folder by folder through the emails.

The key to getting all the subfolders was the olParentFolder assignment.

–e.g. get 2nd level deep subfolder passing in mailbox and an array of subfolder levels – you@email.com, Inbox, 1stSubfolder, 2ndSubfolder
olParentFolder = olNs.Folders(mailbox).Folders(folder(0)).Folders(folder(1))

1 Like