Microsoft KB Archive/291423

= OL2002: ItemRemove Event Does Not Run in Some Scenarios =

Article ID: 291423

Article Last Modified on 3/1/2004

-

APPLIES TO


 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q291423





SYMPTOMS
You use the ItemRemove event in an Outlook solution, but the event does not run.



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 = &quot;Contacts&quot; And Folder.Items.Count = 0 Then MsgBox &quot;Last item in Contacts folder deleted.&quot; 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 = &quot;Contacts&quot; Then If Folder.Items.Count < iNumContacts Then MsgBox &quot;One or more contacts were deleted.&quot; 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:

http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/exchserv/events_4rg4.htm

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.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
This same type of limitation affects the ItemAdd event. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

290653 OL2002: ItemAdd Event Doesn't Run in Some Scenarios

