Accessing Folders

Accessing Folders

Folders can be organized in a hierarchy, allowing you to access folders within folders. Subfolders appear in the Folders collection returned by the Folders property of the Folder object containing them.

With the CDO Library version 1.1 and later, you can create a new folder within an existing folder using the Add method of the Folders collection.

There are two general approaches for accessing folders:

  • Obtaining the folder directly by calling the Session object’s GetFolder method.
  • Navigating folders using the Folders collection’s Get methods.

To obtain the folder directly using the GetFolder method, you must have the folder’s identifier. In the following code fragment, the identifier is stored in the variable strFolderID:

Function Session_GetFolder()
    On Error GoTo error_olemsg

    If objSession Is Nothing Then
        MsgBox "No active session, must log on"
        Exit Function
    End If
    If strFolderID = "" Then
        MsgBox ("Must first set folder ID variable; see Folder->ID")
        Exit Function
    End If
    Set objFolder = objSession.GetFolder(strFolderID)
    ' equivalent to:
    ' Set objFolder = objSession.GetFolder(folderID:=strFolderID)
    If objFolder Is Nothing Then
        Set objMessages = Nothing
        MsgBox "Unable to retrieve folder with specified ID"
        Exit Function
    End If
    MsgBox "Folder set to " & objFolder.Name
    Set objMessages = objFolder.Messages
    Exit Function

error_olemsg:
    MsgBox "Error " & Str(Err) & ": " & Error$(Err)
    Set objFolder = Nothing
    Set objMessages = Nothing
    MsgBox "Folder is no longer available"
    Exit Function
End Function
 

To navigate through the hierarchy of folders, start with a known or available folder, such as the Inbox or Outbox, and examine its Folders collection. You can use the collection’s GetFirst and GetNext methods to get each Folder object in the collection. When you have a subfolder, you can examine its properties, such as its name, to see whether it is the desired folder. The following code fragment navigates through all existing subfolders of the Inbox:

Function TestDrv_Util_ListFolders()
    On Error GoTo error_olemsg
    If objFolder Is Nothing Then
        MsgBox "Must select a folder object; see Session menu"
        Exit Function
    End If
    If 2 = objFolder.Class Then ' verify object is a Folder
            ' with CDO Library 1.1, can use Class value:
            ' If CdoFolder = objFolder.Class Then
        x = Util_ListFolders(objFolder) ' use current global folder
    End If
    Exit Function

error_olemsg:
    MsgBox "Error " & Str(Err) & ": " & Error$(Err)
    Resume Next

End Function

' Function: Util_ListFolders
' Purpose: Recursively list all folders below the current folder
' See documentation topic: Folders collection
Function Util_ListFolders(objParentFolder As Object)
Dim objFoldersColl As Folders ' the child Folders collection
Dim objOneSubfolder As Folder ' a single Folder object
    On Error GoTo error_olemsg
    If Not objParentFolder Is Nothing Then
        MsgBox ("Folder name = " & objParentFolder.Name)
        Set objFoldersColl = objParentFolder.Folders
        If Not objFoldersColl Is Nothing Then ' loop through all
            Set objOneSubfolder = objFoldersColl.GetFirst
            While Not objOneSubfolder Is Nothing
                x = Util_ListFolders(objOneSubfolder)
                Set objOneSubfolder = objFoldersColl.GetNext
            Wend
        End If
    End If
    Exit Function

error_olemsg:
    MsgBox "Error " & Str(Err) & ": " & Error$(Err)
    Resume Next
End Function
 

See Also

Concepts

Searching for a Folder
Programming Tasks