score:4

Accepted answer

You don't have to.

I was curious about your question so I opened up Outlook and wrote this code in ThisOutlookSession:

Private WithEvents items As Outlook.items

Public Sub SetItems()
    Set items = Application.GetNamespace("MAPI") _
                           .GetDefaultFolder(olFolderDeletedItems) _
                           .items
End Sub

Private Sub items_ItemAdd(ByVal Item As Object)

    Dim mail As MailItem

    On Error Resume Next
        Set mail = Item
        Err.Clear
    On Error GoTo 0

    If Not mail Is Nothing Then
        MsgBox mail.Subject
        mail.UnRead = False
    End If

End Sub

Then I ran SetItems from the immediate pane, went to my inbox and deleted a SMS message - as expected mail was Nothing. Then I deleted a single email, and got the message with the mail's subject.

When I selected two emails and hit Delete, the event was fired once for each selected email, so I saw two message boxes - it just works! :)

The Outlook API doesn't seem to offer an event which would handle all deletions at once.

score:0

i have (almost) exactly the same code and it works also for multiple items - only after sleep-mode Outlook seems to forget how to handle deleted items...

Option Explicit
Public WithEvents itDeleted As items

Private Sub Application_Startup()
    Set itDeleted = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems).items
End Sub

Private Sub itDeleted_ItemAdd(ByVal Item As Object)
'MsgBox "deleted-sub fired" 'this only for test-purposes
If TypeOf Item Is MailItem Then
  Item.UnRead = False
End If
End Sub

I think the difference in the definition of "deletedItems" is the problem; that you are not checking the mailitem-property is also not optimal.

Hope this helps, Max


Related Query

More Query from same tag