Walter
Walter

Reputation: 181

MailItem.Delete refusing to delete item in Deleted Items folder

Not sure why the following code is not permanently deleting the MailItem from the Deleted Items folder.

The MailItem certainly ends up in the Deleted Items folder, but the Delete seems to do nothing. Placing a second Delete causes an exception as you would expect because it's not there.

I can manually delete the MailItem permanently via Outlook.

Code:

Dim oOLapp as Outlook.Application
Dim oMapi as Outlook.NameSpace
Dim oFolder as Outlook.Folder
Dim oMailItem as Outlook.MailItem

oOLapp = GetObject([Class]:="Outlook.Application")
oMapi = oOLapp.GetNameSpace("MAPI")
oFolder = oMapi.oFolders(oMapi.DefaultStore.Displayname).Folders("Deleted Items")

oMailItem = oOLapp.Session.OpenSharedItem("C:\sometestmail.msg")
oMailItem = oMailItem.Move(oFolder) ' Now in Deleted items Folder. Verified
' Do other stuff with email
' .
' .
' .
oMailItem.Delete()
oMailItem = nothing

Upvotes: 0

Views: 1326

Answers (1)

Bugs
Bugs

Reputation: 4489

To permanently delete a MailItem you have to loop through the Items within the Deleted folder.

To do this have a look at the following code:

Dim oApp As New Outlook.Application

Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")

Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.UserProperties.Add("DeleteMe", Outlook.OlUserPropertyType.olText)
oMailItem.Move(oFolder)
oMailItem = Nothing

For Each item As Outlook.MailItem In oFolder.Items
    Dim oProperty As Outlook.UserProperty = item.UserProperties.Find("DeleteMe")

    If oProperty IsNot Nothing Then
        item.Delete()
    End If
Next

My code will differ to yours as I have turned Option Strict On. I suggest you do the same. This will help in the long run.

Note that I am setting a UserProperty to the MailItem before moving it to the Deleted folder. This will help identify that single MailItem that you want to permanently delete. If you want to permanently delete all MailItems in the Deleted folder then this is the code you would need:

Dim oApp As New Outlook.Application

Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")

Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.Move(oFolder)
oMailItem = Nothing

For i = oFolder.Items.Count To 1 Step -1
    CType(oFolder.Items(i), Outlook.MailItem).Delete()
Next

In order for this to work you need to loop through the Deleted folder backwards. Looking at the MSDN documentation it states:

The Delete method deletes a single item in a collection. To delete all items in the Items collection of a folder, you must delete each item starting with the last item in the folder. For example, in the items collection of a folder, AllItems, if there are n number of items in the folder, start deleting the item at AllItems.Item(n), decrementing the index each time until you delete AllItems.Item(1).

Upvotes: 3

Related Questions