VBOffice

Looping Recursively Through Folders and Subfolders

These samples demonstrate the basics for a recursive loop through folders and their subfolders.

Last modified: 2006/01/19 | Accessed: 43.711  | #12
◀ Previous sample Next sample ▶

Content

Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.

Outlook Object Model: List All Folders

With a recursive loop through all the folders you can, for instance, get all their contents without the need to know each folder name. This is useful if at programming time you don't know which or how many folders will be existent. This sample demonstrates the basics, which can easily be reused for different needs.


tip  How to add macros to Outlook
Public Sub LoopFolders(Folders As Outlook.Folders, _
  ByVal Recursive As Boolean _
)
  Dim Folder As Outlook.MAPIFolder

  For Each Folder In Folders
    DoAnything Folder

    If Recursive Then
      LoopFolders Folder.Folders, Recursive
    End If
  Next
End Sub

Private Sub DoAnything(Folder As Outlook.MAPIFolder)
  Debug.Print Folder.Name
End Sub

Outlook Object Model: List All Folder Contents

This one uses the OOM to list all the contents of all folders.

Public Sub LoopFolders(Folders As Outlook.Folders, _
  ByVal Recursive As Boolean _
)
  Dim Folder As Outlook.MAPIFolder

  For Each Folder In Folders
    LoopItems Folder.Items

    If Recursive Then
      LoopFolders Folder.Folders, Recursive
    End If
  Next
End Sub

Private Sub LoopItems(Items As Outlook.Items)
  Dim obj As Object

  For Each obj In Items
    If TypeOf obj Is Outlook.MailItem
      DoAnything obj
    End If
  Next
End Sub

Private Sub DoAnything(Item As Outlook.MailItem)
  Debug.Print Item.Subject
End Sub
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

CDO 1.21: List All Folders

The samples above for the OOM work with all versions of Outlook, however, up to and including Outlook 2003 they are pretty slowly. For these versions of Outlook the CDO 1.21 library is much faster.

Public Sub LoopFolders(Folders As MAPI.Folders, _
  ByVal Recursive As Boolean _
)
  Dim Folder As MAPI.Folder

  For Each Folder In Folders
    DoAnything Folder

    If Recursive Then
      LoopFolders Folder.Folders, Recursive
    End If
  Next
End Sub

Private Sub DoAnything(Folder As MAPI.Folder)
  Debug.Print Folder.Name
End Sub

CDO 1.21: List All Folder Contents

Here comes the CDO version for listing the folder contents.

Public Sub LoopFolders(Folders As MAPI.Folders, _
  ByVal Recursive As Boolean _
)
  Dim Folder As MAPI.Folder

  For Each Folder In Folders
    LoopItems Folder.Messages

    If Recursive Then
      LoopFolders Folder.Folders, Recursive
    End If
  Next
End Sub

Private Sub LoopItems(Items As MAPI.Messages)
  Dim obj As Object

  For Each obj In Items
    If TypeOf obj Is MAPI.Message
      DoAnything obj
    End If
  Next
End Sub

Private Sub DoAnything(Item As MAPI.Message)
  Debug.Print Item.Subject
End Sub
Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.
email  Send a message