'How can an outlook addin detect when an email is deleted
I have created an Outlook addin that handles attachments. When an attachment is added to a mail item I am processing it and adding a user property containing information about the attachment. I have an item send event handler that does the final processing of the attachment just before the mail is sent.
My main issue at this point is that I need to detect when a mail item is deleted so that I can remove the associated resources especially if it was never sent. The before delete event handler seems like the obvious choice for this but doesn't seem to get called.
Can anybody tell me how I can detect when a mail item is deleted? I guess what I probably need to do is detect when it is removed from the "Deleted Items" folder and deleted rather than just being moved to a different folder.
Solution 1:[1]
The BeforeDelete event of Outlook items is really what you are looking for.
Public WithEvents myItem As Outlook.MailItem
Public Sub DeleteMail()
Const strCancelEvent = "Application-defined or object-defined error"
On Error GoTo ErrHandler
Set myItem = Application.ActiveInspector.CurrentItem
myItem.Delete
Exit Sub
ErrHandler:
MsgBox Err.Description
If Err.Description = strCancelEvent Then
MsgBox "The event was cancelled."
End If
'If you want to execute the next instruction
Resume Next
'Otherwise it will finish here
End Sub
Private Sub myItem_BeforeDelete(ByVal Item As Object, Cancel As Boolean)
'Prompts the user before deleting an item
Dim strPrompt As String
'Prompt the user for a response
strPrompt = "Are you sure you want to delete the item?"
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbNo Then
'Don't delete the item
Cancel = True
End If
End Sub
To distinguish between moving to the Deleted Items folder and removing permanently you can add an ItemAdd
event to the Deleted Items folder.
Solution 2:[2]
There is no way to easy catch anything related to message deletion. BeforeDelete
does not reliably work, plus an item can get deleted when your code is not running (e.g. when a user deletes it from another computer / phone /etc.).
Also keep in mind that all MAPI events are asynchronous, so by the time you get an event, the item is already gone. On the MAPI level, the folder contents table raises TABLE_ROW_DELETED
notification, but since the message is already gone, you only get the value of the PR_INSTANCE_KEY
property. It can only be useful if you already know PR_INSTANCE_KEY
of the particular message or if you have a list of PR_INSTANCE_KEY
for all messages in the folder. You can see the event fire in OutlookSpy (I am its author - click IMAPIFolder button, go to the GetContentsTable tab, look at the log at the bottom of the window). If MAPI (C++ or Delphi) is not an option, Redemption (I am also its author) exposes the RDOItems.ItemRemove
event, which passes PR_INSTANCE_KEY
as the parameter.
Another option under Exchange Server is the Incremental Change Synchronization API. Again, it is pure MAPI and requires C++ or Delphi. Redemption exposes it through the RDOFolderSynchronizer object.
The only generic option is to periodically scan the messages in the folder and check them against your own list to see if any items are missing. This can be a performance bottleneck, even if you optimize its by using the Outlook.Table object to retrieve properties from multiple items in a single call.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eugene Astafiev |
Solution 2 |