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.
Dim WithEvents ofcFolders As Folders Private Sub Class_Initialize() Set ofcFolders = Application.GetNamespace("MAPI"). _ GetDefaultFolder(olFolderDeletedItems).Folders End Sub
The FolderAdd event occurs when a new MAPIFolder object is added to a collection.
Sub FoldersCollection_FolderAdd(ByVal Folder As MAPIFolder)
|Folder||MAPIFolder||The new MAPIFolder object that is being added to the Folders collection.|
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.
The FolderChange event occurs when one of the MAPIFolder objects, in the chosen Folders collection, is changed.
Sub FoldersCollection_FolderChange(ByVal Folder As MAPIFolder)
|Folder||MAPIFolder||The MAPIFolder object that is being changed to the Folders collection.|
The FolderRemove event occurs when one of the MAPIFolder objects is deleted from the chosen Folders collection.
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
Set ofFolder = onMAPI.GetFolderFromID(sEntryID, sStoreID)
Set ofFolder = onMAPI.GetDefaultFolder(olFolderInbox)
Set ofFolder = onMAPI.GetSharedDefaultFolder(orRecipient, olFolderInbox)
Ser ofFolder = onMAPI.Folders.Item("Inbox")
set ofFolder = onMAPI.Folders.GetFirst
These methods are covered fully in chapter 6 and earlier in this chapter.
Page 4 of 6