2

I have the below code to permanently delete mail from the inbox.

However, when responses to a meeting invite, to say the person has accepted the meeting do not delete.

When I click on that mail and run this code it does not delete?

Sub PermDelete(Item As Outlook.MailItem)
    ' First set a property to find it again later
    Item.UserProperties.Add "Deleted", olText
    Item.Save
    Item.Delete
    
    'Now go through the deleted folder, search for the property and delete item
    Dim objDeletedFolder As Outlook.Folder
    Dim objItem As Object
    Dim objProperty As Variant
    
    Set objDeletedFolder = Application.GetNamespace("MAPI"). _
      GetDefaultFolder(olFolderDeletedItems)
    For Each objItem In objDeletedFolder.items
        Set objProperty = objItem.UserProperties.Find("Deleted")
        If TypeName(objProperty) <> "Nothing" Then
            objItem.Delete
        End If
    Next
    
End Sub
Community
  • 1
  • 1
Lalaland
  • 306
  • 1
  • 8
  • Does this answer your question? [When is a MailItem not a MailItem?](https://stackoverflow.com/questions/78924/when-is-a-mailitem-not-a-mailitem) – niton Dec 07 '21 at 20:27
  • If declaring as object was insufficient, are the response objects in the same Inbox as the mailitems? – niton Dec 12 '21 at 15:09
  • Edit the question to add the code for passing Item to `PermDelete`. – niton Dec 12 '21 at 18:03
  • That code i am using in outlook is the only code im using Niton. – Lalaland Dec 13 '21 at 16:26
  • Go back to the start. Edit the question to describe how successfully deleted mailitems get to `PermDelete`. – niton Dec 13 '21 at 17:28
  • So in the code you can see, it first adds a property to the item, then deletes the mail to the normal deleted item folder, then the next part of the code goes to the deleted folder and deletes from there. (perm delete) – Lalaland Dec 14 '21 at 18:40
  • Is this code run from a rule so you do not select mailitems? – niton Dec 14 '21 at 18:46
  • Hi there, no I select the item then click a button to run the code. – Lalaland Dec 15 '21 at 19:42

3 Answers3

1

To run code that has an argument like (Item As Outlook.MailItem) you need to pass the information in this case Item.

You cannot run such code from a button.

You can run Sub delItemPermanently() from a button or F8 to step through.

Option Explicit

Sub delItemPermanently()
    ' Select a single item
    ' This line passes the item to PermDelete
    PermDelete ActiveExplorer.Selection(1)
End Sub


Sub PermDelete(Item As Object)

    ' Notice Object not Mailitem
    ' This will accommodate mailitems as well
    
    ...

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
0

In the code your function accepts an instance of the MailItem class only. But an Outlook folder may contain different types of items - appointments, documents, notes and etc. To differentiate them at runtime you can use the following construction:

Dim obj As Object

If TypeName(obj) = "MailItem" Then
  ' your code for mail items here
End If

So, you need to declare the function in the following way (if you don't need to do separate actions for different kind of items):

Sub PermDelete(Item As Object)
' First set a property to find it again later
Item.UserProperties.Add "Deleted", olText
Item.Save
Item.Delete

'Now go through the deleted folder, search for the property and delete item
Dim objDeletedFolder As Outlook.Folder
Dim objItem As Object
Dim objProperty As Variant

Set objDeletedFolder = Application.GetNamespace("MAPI"). _
  GetDefaultFolder(olFolderDeletedItems)
For Each objItem In objDeletedFolder.items
    Set objProperty = objItem.UserProperties.Find("Deleted")
    If TypeName(objProperty) <> "Nothing" Then
        objItem.Delete
    End If
Next

End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • thanks Eugene, i tried that as i really dont care what item type it is but that is still not working. Calendar responses are not deleting sadly. – Lalaland Dec 08 '21 at 10:09
0

Set Item to a generic Object

Example on selected item

Option Explicit
Public Sub Example()
    Dim obj As Object
    
    Set obj = ActiveExplorer.Selection.Item(1)
        obj.Delete

End Sub
0m3r
  • 12,286
  • 15
  • 35
  • 71