Working with Folders

An email client application can perform a number of tasks with Folder objects. An obvious one is to locate a folder of interest within the folder hierarchy. The Session object maintains references to the Inbox and Outbox folders of the default InfoStore, making it easy to obtain either. However, the Session object doesn’t maintain references to other common folders, so it’s useful to implement a generic function for finding a folder by name. Example 7-1 shows such a function.

Example 7-1. Finding a Folder Object by Name

Public Function GetFolderByName( _ ByVal CdoSession As MAPI.Session, _ ByVal strFolderName As String, _ Optional ByVal CdoFolderParent As MAPI.Folder = Nothing, _ Optional ByVal bCreate As Boolean = True _ ) As MAPI.Folder Dim CdoInfoStore As MAPI.InfoStore Dim CdoFolderRoot As MAPI.Folder Dim CdoFolders As MAPI.Folders Dim CdoFolder As MAPI.Folder Dim bFound As Boolean ' If the parent folder wasn't passed in, then use the root ' folder of the default InfoStore. If CdoFolderParent Is Nothing Then ' Get the Folders collection from the default InfoStore. Set CdoInfoStore = CdoSession.GetInfoStore Set CdoFolderRoot = CdoInfoStore.RootFolder Set CdoFolders = CdoFolderRoot.Folders Else ' Get the Folders collection from the parent folder. Set CdoFolders = CdoFolderParent.Folders End If ' Loop through the folders in the collection until the ' desired folder is found. bFound = False Set CdoFolder = CdoFolders.GetFirst Do While (Not bFound) And Not ...

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.