---
description: If there are sub folders within main Inbox or Sent Items folder,using VBA is a recursive and efficient way to traverse them.
title: How to Traverse a Folder Tree Recursively in Outlook via VBA
image: https://www.datanumen.com/blogs/wp-content/uploads/2016/12/folder-tree-in-outlook.jpg
---

-
-
-

[Home](https://www.datanumen.com/blogs/) > [Solutions](https://www.datanumen.com/blogs/category/solutions/) > [Office Solutions](https://www.datanumen.com/blogs/category/solutions/office-solutions/) > [Outlook Solutions](https://www.datanumen.com/blogs/category/solutions/office-solutions/outlook-solutions/) > How to Traverse a Folder Tree Recursively in Outlook via VBA

# How to Traverse a Folder Tree Recursively in Outlook via VBA

[Outlook Solutions](https://www.datanumen.com/blogs/category/solutions/office-solutions/outlook-solutions/) September 29, 2025

Share Now:

-
-
-
-
-
-
-
-

*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 Outlook](https://www.datanumen.com/blogs/wp-content/uploads/2016/12/folder-tree-in-outlook.jpg "Folder Tree In Outlook")Corporate 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]( "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](https://www.datanumen.com/outlook-repair/) 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](https://www.datanumen.com/dwg-recovery/) and rar recovery software products. For more information visit [www.datanumen.com](https://www.datanumen.com/)

Share Now:

-
-
-
-
-
-
-
-

[← Previous post](https://www.datanumen.com/blogs/auto-assign-color-category-new-appointments-via-outlook-vba/)

[Next post →](https://www.datanumen.com/blogs/how-to-digitally-sign-your-word-macro-project/)

Comments are closed.

![Data Recovery Blog]()

Follow or like us on Facebook, LinkedIn and Twitter to get all promotions, latest news and updates on our products and company.

-
-
-

### Featured Products

- [DataNumen Outlook Repair](https://www.datanumen.com/outlook-repair/)
- [DataNumen SQL Recovery](https://www.datanumen.com/sql-recovery/)
- [DataNumen Exchange Recovery](https://www.datanumen.com/exchange-recovery/)
- [DataNumen Access Repair](https://www.datanumen.com/access-repair/)
- [DataNumen Excel Repair](https://www.datanumen.com/excel-repair/)
- [DataNumen PDF Repair](https://www.datanumen.com/pdf-repair/)
- [DataNumen Word Repair](https://www.datanumen.com/word-repair/)
- [DataNumen Zip Repair](https://www.datanumen.com/zip-repair/)

### Popular posts

```json
{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/#article","isPartOf":{"@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/"},"author":{"name":"AuthorCCW","@id":"https:\/\/www.datanumen.com\/blogs\/#\/schema\/person\/33ded6c47c77587f9158d99f05e23957"},"headline":"How to Traverse a Folder Tree Recursively in Outlook via VBA","datePublished":"2016-12-10T03:20:09+00:00","dateModified":"2025-09-29T14:18:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/"},"wordCount":529,"commentCount":0,"publisher":{"@id":"https:\/\/www.datanumen.com\/blogs\/#organization"},"image":{"@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanumen.com\/blogs\/wp-content\/uploads\/2016\/12\/folder-tree-in-outlook.jpg","articleSection":["Outlook Solutions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/","url":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/","name":"How to Traverse a Folder Tree Recursively in Outlook via VBA","isPartOf":{"@id":"https:\/\/www.datanumen.com\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/#primaryimage"},"image":{"@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanumen.com\/blogs\/wp-content\/uploads\/2016\/12\/folder-tree-in-outlook.jpg","datePublished":"2016-12-10T03:20:09+00:00","dateModified":"2025-09-29T14:18:49+00:00","description":"If there are sub folders within main Inbox or Sent Items folder,using VBA is a recursive and efficient way to traverse them.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanumen.com\/blogs\/traverse-folder-tree-recursively-outlook-via-vba\/#primaryimage","url":"https:\/\/www.datanumen.com\/blogs\/wp-content\/uploads\/2016\/12\/folder-tree-in-outlook.jpg","contentUrl":"https:\/\/www.datanumen.com\/blogs\/wp-content\/uploads\/2016\/12\/folder-tree-in-outlook.jpg","width":274,"height":270,"caption":"Folder Tree In Outlook"},{"@type":"WebSite","@id":"https:\/\/www.datanumen.com\/blogs\/#website","url":"https:\/\/www.datanumen.com\/blogs\/","name":"Data Recovery Blog","description":"Discuss every aspect of data recovery","publisher":{"@id":"https:\/\/www.datanumen.com\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanumen.com\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanumen.com\/blogs\/#organization","name":"Data Recovery Blog","url":"https:\/\/www.datanumen.com\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanumen.com\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanumen.com\/blogs\/wp-content\/uploads\/2019\/02\/logowithouttext.jpg","contentUrl":"https:\/\/www.datanumen.com\/blogs\/wp-content\/uploads\/2019\/02\/logowithouttext.jpg","width":296,"height":397,"caption":"Data Recovery Blog"},"image":{"@id":"https:\/\/www.datanumen.com\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataNumen","https:\/\/x.com\/DataNumen","http:\/\/www.linkedin.com\/company\/datanumen","https:\/\/myspace.com\/datanumen\/","http:\/\/www.pinterest.com\/datanumen\/","https:\/\/www.youtube.com\/channel\/UCVIIuuKfUBHSJauUaVP13tw"]},{"@type":"Person","@id":"https:\/\/www.datanumen.com\/blogs\/#\/schema\/person\/33ded6c47c77587f9158d99f05e23957","name":"AuthorCCW","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3516ed90fc242780d3947a989f895bc46e70c3e83485d30f2b457a007906aa58?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3516ed90fc242780d3947a989f895bc46e70c3e83485d30f2b457a007906aa58?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3516ed90fc242780d3947a989f895bc46e70c3e83485d30f2b457a007906aa58?s=96&d=mm&r=g","caption":"AuthorCCW"},"sameAs":["https:\/\/www.datanumen.com\/"]}]}
```
