Microsoft Office Tutorials and References
In Depth Information
LISTING 24.1
A Function to Obtain a Reference to a User-Created Folder
Public Function FindFolder(FolderName As String) _
As Outlook.Folder
Dim folder1 As Outlook.Folder
Dim folder2 As Outlook.Folder
Dim folder3 As Outlook.Folder
Dim FoundFolder As Outlook.Folder
Dim MyOutlookNamespace As Outlook.NameSpace
Set MyOutlookNamespace = GetNamespace(“MAPI”)
Set FindFolder = Nothing
For Each folder1 In MyOutlookNamespace.Folders
‘ We know that top-level folders will not match
‘ so no comparison is needed here.
For Each folder2 In folder1.Folders
If folder2.Name = FolderName Then
Set FindFolder = folder2
Exit Function
End If
For Each folder3 In folder2.Folders
If folder3.Name = FolderName Then
Set FindFolder = folder3
Exit Function
End If
Next
Next
Next
End Function
Working with Folder Items
Every folder can contain items, and the purpose for getting a reference to an Outlook folder is
almost always to do something with the items that it contains. With user-defined folders, you
specify the type of Outlook item the folder will contain when you create the folder. When you are
working with a folder in VBA, you may want to determine its default type of item. For example,
perhaps you are writing a macro that will look for certain information in all email messages in all
folders. You can iterate through all Outlook folders using the techniques described earlier in this
chapter, and then process the folder items only if its default item type is email.
To determine a folder’s default item type, look at its DefaultItemType property. The values that
can be returned are represented by named constants as described in Table 24.2. This property is
read-only, which means that you cannot change it.
Search JabSto ::




Custom Search