StartDownloadsServiceSamplesWorkshopsContact DeutschEnglish
 
Samples
General
Outlook®
 
Awarded by
Microsoft since 2005:
mvp logo
VBOffice Info
Visitors1777151
Impressions6663253
Links
Imprint
Privacy Policy
Contact
Looping recursively through folders and subfolders
Author: Michael BauerHomepage
Date: 19.01.2006Accessed: 41254
  
Description

These samples demonstrate the basics for a recursive loop through folders and their subfolders. The processing of a folder's content is separated so that it's easier to reuse the code.

The first sample uses the Outlook object model, which is quite slowly up to and including Outlook 2003. Much faster is the use of the CDO 1.21 library then, which is shown in the second sample.

(Because the procedure names are identically in both the samples, you can only use one of it in the same module.)

' <Sample #1: Outlook objects>
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
    Select Case True
    Case TypeOf obj Is Outlook.MailItem
      HandleMailItem obj
    Case TypeOf obj Is Outlook.ContactItem
      ' ...
    End Select
  Next
End Sub

Private Sub HandleMailItem(Item As Outlook.MailItem)
  ' ...
End Sub
' </Sample #1>

' <Sample #2: MAPI objects>
Public Sub LoopFolders(oFolders As MAPI.Folders, _
  ByVal bRecursive As Boolean _
)
  Dim oFld As MAPI.Folder

  For Each oFld In oFolders
    LoopItems oFld

    If bRecursive Then
      LoopFolders oFld.Folders, bRecursive
    End If
  Next
End Sub

Private Sub LoopItems(oFld As MAPI.Folder)
  Dim obj As Object
  Dim oItems As MAPI.Messages

  Set oItems = oFld.Messages
  For Each obj In oItems
    Select Case True
    Case (TypeOf obj Is MAPI.Message)
      HandleMessage obj
    Case (TypeOf obj Is MAPI.AppointmentItem)
      ' ...
    End Select
  Next
End Sub

Private Sub HandleMessage(oItem As MAPI.Message)
  ' ...
End Sub
' </Sample #2>
 
 

ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the ... [more]

 

Access the master category list in the blink of an eye, share your categories in a network, get a reminder service, and ... [more]

 

SAM automatically sets the sender, signature, and folder for sent items, for instance based on the recipient ... [more]

 

OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or ... [more]