CAUSE
The event is designed to parallel the MAPI function
ROW_DELETE, but there are scenarios where MAPI issues a
TABLE_CHANGED notification instead of a
ROW_DELETE notification when items are deleted. Either of the following scenarios prevents the ItemRemove event running:
- In a personal folder (.pst) file, the last item in a folder is deleted.
- In a .pst file, Microsoft Exchange mailbox, or an Exchange public folder, 16 or more items are deleted at once.
WORKAROUND
Use one of the following methods to work around the problem.
Method 1
To work around the limitation of the ItemRemove event not running when the last item in a .pst folder is deleted, you can use the FolderChange event to detect when a change has been made to a folder, and then check to see if the number of items in the folder is zero. If so, this means the last item in the folder was deleted. For example, the following Outlook Visual Basic for Applications code monitors the default Contacts folder, and displays a message when the last contact is deleted.
Dim WithEvents objFolders As Outlook.Folders
Sub Application_Startup()
Set objFolders = Session.GetDefaultFolder(olFolderInbox).Parent.Folders
End Sub
Sub objFolders_FolderChange(ByVal Folder As Outlook.MAPIFolder)
If Folder.Name = "Contacts" And Folder.Items.Count = 0 Then
MsgBox "Last item in Contacts folder deleted."
End If
End Sub
You can also determine when more than 16 items have been deleted. To do this, keep track of the number of items in the folder, and then in the FolderChange event, check to see if there are fewer items than there previously were. For example, the following Outlook Visual Basic for Applications code monitors the default Contacts folder, and displays a message when contacts are deleted.
Dim WithEvents objFolders As Outlook.Folders
Dim objConFolder As Outlook.MAPIFolder
Dim iNumContacts As Long
Sub Application_Startup()
Set objFolders = Session.GetDefaultFolder(olFolderInbox).Parent.Folders
Set objConFolder = Session.GetDefaultFolder(olFolderContacts)
iNumContacts = objConFolder.Items.Count
End Sub
Sub objFolders_FolderChange(ByVal Folder As Outlook.MAPIFolder)
If Folder.Name = "Contacts" Then
If Folder.Items.Count < iNumContacts Then
MsgBox "One or more contacts were deleted."
End If
iNumContacts = objConFolder.Items.Count
End If
End Sub
Method 2
Depending on your solution's needs, implement your own custom algorithm to determine when items have been deleted. You may want to implement the ItemRemove event to handle most deletions, and then run some other code on a timer to trap deletions that are not detected by the ItemRemove event.
Method 3
If you are using Microsoft Exchange Server 5.5 Service Pack 1 (SP1) or later, you can implement an Exchange Server Scripting Agent script if the folder is located in an Exchange Server public folder or mailbox. In the script, you can implement the OnMessageDeleted event. However, you must use the Collaboration Data Objects (CDO) object model in the server script, and not the Outlook object model; therefore, this approach may not be feasible for your solution. For additional information about the Exchange Server Scripting Agent, see the following Microsoft Web site:
For additional information about how to use the OnMessageDeleted event in a server script, click the article number below
to view the article in the Microsoft Knowledge Base:
231160 HOWTO: Relate a Deleted Message Using Folder_OnMessageDeleted
IMPORTANT: Exchange Server 5.x scripts do not scale well if they are applied to multiple mailboxes. Performance varies greatly depending on many factors, but do not consider applying scripts to more than about 10 to 20 mailboxes.