Outlook 2000 VBA Programmers Reference
For the Folders collection events to be fired it is necessary to use the WithEvents keyword and set a reference to a Folders collection within a sub that will be called before the event. For illustration purposes only I have set a reference to the Deleted Items folders collection.
Example:
Dim WithEvents ofcFolders As Folders Private Sub Class_Initialize() Set ofcFolders = Application.GetNamespace("MAPI"). _ GetDefaultFolder(olFolderDeletedItems).Folders End Sub
FolderAdd Event
The FolderAdd event occurs when a new MAPIFolder object is added to a collection.
Sub FoldersCollection_FolderAdd(ByVal Folder As MAPIFolder)
Name | Data type | Description |
Folder | MAPIFolder | The new MAPIFolder object that is being added to the Folders collection. |
Example:
Private Sub ofcFolders_FolderAdd(ByVal Folder As MAPIFolder) If Folder.UnReadItemCount <> 0 Then MsgBox "This folder has unread items. You may _ want to reinstate it.", vbExclamation End If End Sub
In this example the user is warned if they delete a folder with unread items in it. Firstly ofcFolders is set to be the Deleted Items Folders collection. This will be called when an object based on this class is instantiated.
Within the FolderAdd event, a newly created folder is checked for unread items. If they exist, the user is warned.
FolderChange Event
The FolderChange event occurs when one of the MAPIFolder objects, in the chosen Folders collection, is changed.
Sub FoldersCollection_FolderChange(ByVal Folder As MAPIFolder)
Name | Data type | Description |
Folder | MAPIFolder | The MAPIFolder object that is being changed to the Folders collection. |
FolderRemove Event
The FolderRemove event occurs when one of the MAPIFolder objects is deleted from the chosen Folders collection.
Sub FoldersCollection_FolderRemove()
MAPIFolder Object
A MAPIFolder object is a container that can hold either folders or Outlook items, or more likely both. A reference to a MAPIFolder object can be set by using the:
GetDefaultFolder, GetFolderFromID and GetSharedDefaultFolder method s of the NameSpace object
Item, GetFirst, GetLast, GetNext and GetPrevious methods of a Folders collection
Syntax:
Set ofFolder = onMAPI.GetFolderFromID(sEntryID, sStoreID)
or
Set ofFolder = onMAPI.GetDefaultFolder(olFolderInbox)
or
Set ofFolder = onMAPI.GetSharedDefaultFolder(orRecipient, olFolderInbox)
or
Ser ofFolder = onMAPI.Folders.Item("Inbox")
or
set ofFolder = onMAPI.Folders.GetFirst
These methods are covered fully in chapter 6 and earlier in this chapter.
Page 4 of 6
This article was originally published on November 20, 2002