How to Traverse a Folder Tree Recursively in Outlook via VBA

If there are sub folders within main Inbox or Sent Items folder, how to go about traversing them in a recursive and efficient way, using VBA, is a very useful tip. This critical piece is reusable in many other VBA projects.

The Critical Problem of Traversing Folders

Folder Tree In OutlookCorporate and commercial users of Outlook often end up having hundreds of subfolders beneath their main inbox or sent items folder, with relevant emails in each of those. When there comes a need to go through all those folders to perform some action on the emails contained in those, it becomes a nightmare to write different VBA scripts for different folders. Imagine having five hundred folders beneath the main Inbox folder and a VBA script requirement for each of those. The user will have to write five hundred unique scripts for each of those folders, with the similar functionality. To make it worse, if there comes a need to make some modifications, all five hundred scripts will be required to be updated. Luckily there is a programming methodology known as recursion, which solves our problem like a breeze. The idea of recursion is for a function to repeatedly call itself and go through the same pattern again and again, in order to process all the items, with the same logic. Below is a sample script which starts from the main Inbox folder and iterates through all the subfolders and items in each of those folders.

Outlook VBA Script

Below is the complete Outlook VBA script:

Private Sub Main()
    Dim objNameSpace As Outlook.NameSpace
    Dim objMainFolder As Outlook.Folder
    
    Set objNameSpace = Application.GetNamespace("MAPI")
    Set objMainFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
    
    Call ProcessCurrentFolder(objMainFolder)
End Sub
 
Private Sub ProcessCurrentFolder(ByVal objParentFolder As Outlook.MAPIFolder)
    Dim objCurFolder As Outlook.MAPIFolder
    Dim objMail As Outlook.MailItem
    
    On Error Resume Next
    
    '    Process each items in the folder
    For Each objMail In objParentFolder.Items
        '    Do your task here ...
    Next
    
    '    Process the subfolders in the folder recursively
    If (objParentFolder.Folders.Count > 0) Then
        For Each objCurFolder In objParentFolder.Folders
            Call ProcessCurrentFolder(objCurFolder)
        Next
    End If
End Sub

How to Run the Script

Press Alt + F11 to open the Outlook VBA editor and paste the above code in “ThisOutlookSession”. It is important to know that prior to running this script, Microsoft Outlook XX Object library should be added to the project references from “Tools” menus, where XX is the library version.

Understand the Script

Recursion Function“ProcessCurrentFolder” is the recursive function, which calls itself, if a subfolder is discovered within the current folder. It will keep on recalling itself unless all the folders starting from initial hierarchy are scanned and processed. This script will work with any Outlook folder. “olFolderInbox” can be replaced with “olFolderSentMail”, “olFolderOutbox”, or can be any other non-default Outlook folder. It is also important to know that it is not necessary to make the initial call from the root inbox or outbox folder but it can start working from any folder downwards. “For Each objMail In objParentFolder.Items” is the key line which will iterate through all the items contained in the current folder being scanned; therefore that is the place where the user needs to put the required code to process the items.

Recover from Disaster

If you are faced with a situation of Outlook crash or failure, there is no need to worry about the lost emails, as DataNumen shall brings your life back to normal by fixing Outlook corruption in an efficient way.

Author Introduction:

Mary Underwood is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including dwg recovery and rar recovery software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published. Required fields are marked *