Delete email from inbox and also delete it from deleted-items folder via rule->script

10,990

The problem was not recreated, but try stepping through this then run normally if it appears to do what you want.

Sub doWorkAndDeleteMail(Item As mailitem)
Dim currFolder As Folder
Dim DeletedFolder As Folder
Dim i As Long
Dim mySubject As String
Set currFolder = ActiveExplorer.CurrentFolder
mySubject = Item.Subject
Debug.Print mySubject
Set DeletedFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems)
Set ActiveExplorer.CurrentFolder = DeletedFolder
Debug.Print "DeletedFolder.count before delete: " & DeletedFolder.Items.count
' delete email from deleted items-folder
Item.Delete
Debug.Print "DeletedFolder.count  after delete: " & DeletedFolder.Items.count
' If necessary
'DoEvents
For i = DeletedFolder.Items.count To 1 Step -1
    Debug.Print DeletedFolder.Items(i).Subject
    If (DeletedFolder.Items(i).Subject) = mySubject Then
        Debug.Print DeletedFolder.Items(i).Subject & " *** found ***"
        DeletedFolder.Items(i).Delete
        Exit For
    End If
Next
Set ActiveExplorer.CurrentFolder = currFolder
End Sub
Share:
10,990
Author by

and0r

Updated on December 01, 2022

Comments

  • and0r 21 days



    I created a rule, that starts a VBA-script depending on the subject of a received email (Rule: Subject "MY_SUBJECT" -> start script).
    The VBA script is then doing some stuff and then it should finally delete the original email.

    This part is easy:

    Sub doWorkAndDeleteMail(Item As Outlook.MailItem)
    ' doSomething:
    ' delete email from inbox
    Item.Delete
    End Sub
    


    Now the email will sit in the deleted-items-folder. But what I need to achieve is, to also delete this mail from the deleted-items folder. Since I know the subject of this mail (because this triggered my rule in the first place), I tried the following approach:

    Sub doWorkAndDeleteMail(Item As Outlook.MailItem)
    ' doSomething:
    ' delete email from inbox
    Item.Delete
    End Sub
    ' delete email from deleted items-folder
    Dim deletedFolder As Outlook.Folder
    Set deletedFolder = Application.GetNamespace("MAPI"). _
        GetDefaultFolder(olFolderDeletedItems)
    Dim i As Long
    For i = myFolder.Items.Count To 1 Step -1
    If (deletedFolder.Items(i).Subject) = "MY_SUBJECT" Then
    deletedFolder.Items(i).Delete
    Exit For
    End If
    Next if
    End Sub
    


    Well, this basically works: The mail with this subject will be found in the deleted-items-folder and it will be deleted, yes. But sadly it does not work as expected: This permanent deletion only works once I start the script a second time.

    So the email which is triggering my script will never be deleted permanently in this script's actual run, but only in the next run (once the next email with the trigger-subject for my rule is received - but then this very next email won't be deleted, again).

    Do you have any idea what I am doing wrong here? It somehow looks like I need to refresh my deleted-items folder somehow. Or do I have to comit my first Item.Delete somehow explicitly?

    • Tim Williams
      Tim Williams almost 6 years
    • and0r almost 6 years
      I have seen that solution before, yes. I don't have RDO or CDO installed (also I want an independet solution). The third apporach there (marking the mail with a property) looks quite the same to mine: It shouldn't matter if I search for a subject or another property. A little more on the bottom of that thread someone is claiming that there is a "remove"-method that will remove a mail item permanently from the very beginning. But this remove-method seems not to exist for the type "Outlook.MailItem" (which is the input of the script). Anyway: I would like to know what is wrong with my approach.
    • and0r almost 6 years
      I tried the setting and searching for a subject: As expected, the behaviour is exactly the same. Deletion works only when running the script the next time.
    • Tim Williams
      Tim Williams almost 6 years
      Did you try the very first method in that answer - moving the item to the Deleted folder and then deleting it?
    • and0r almost 6 years
      @TIm Williams Yes, but this just moves it there. The final deletion does not take place - again it behaves, as if it was not really inside of that folder at that moment....
    • 0m3r
      0m3r almost 6 years
      @and0r see my answer, simple way to automate deleting items as soon its moved to deleted folder
    • 0m3r
      0m3r almost 6 years
      You still having issue?
    • and0r almost 6 years
      @0m3r I will check that out tomorrow. Thanks a lot! As far as I understand from mere reading the code example: Your suggestion is working with a general event handler that is set up for the OutlookSession itself. Well: This could work :) - But still we have no knowledge why my apporach does behave so strangely...
  • and0r almost 6 years
    By the way, I also tried an aproach to wait 5 seconds between "deleting from inbox" and "deleting from deleted-items" using a while loop waiting for ticks (with standard "sleep" the whole application would hang), but also this waiting didn't help here. I am definitely missing something here... :( The deleted-Items folder just seems to not know about that deleted item until the next script run. But why not?
  • 0m3r
    0m3r almost 6 years
    Have you tried itemAdd even to tricker delete when item is moved to deleted folder? Or I once your rule is finish doing something then run loop to remove items from inbox - let me know if you need examples
  • and0r almost 6 years
    @0m3r , I don't know how to do that. An example would be nice. Thanks!
  • 0m3r
    0m3r almost 6 years
    Don't use For Each - If you are deleting you should use For and Step reverse loop
  • and0r almost 6 years
    Thanks for your reply. I copied this script under This Outlook-Session and sent myself an email with a subject "MY_SUBJECT". Then I manually deleted it from the inbox. But it still shows up in the delete items folder. This is strange. Do I have to activate events somehow?
  • 0m3r
    0m3r almost 6 years
    @and0r Have you restarted your Outlook once you added the code
  • and0r almost 6 years
    Yes I did. Does it work for you? I am using Office 365 (Outlook v16.0...)
  • 0m3r
    0m3r almost 6 years
    @and0r Oh I'm on 2010, try to copy my loop to your code and see if that helps
  • and0r almost 6 years
    Thanks for your edit but your second proposal is again in the area of that, what I did before. And the behaviour is now again that it will move the mail to "deleted items" but it won't delete it there. It will only delete it there, after another (a second) mail with that subject has been received. Of couse this second email will be inside of "delete items" after that. This wil only be deleted, when a third mail arrives...and so on
  • and0r almost 6 years
    The problem is not the kind of loop IMHO. It is, that the item seems not to be really inside "deletedd items". Maybe it has something to do, that this scribt is triggered via a rule?
  • 0m3r
    0m3r almost 6 years
    @and0r what happens if you change Item.Delete to Item.Remove
  • and0r almost 6 years
    then I get "Object doesnt support this property or method". I also tried that before :) But Outlook.MailItem does not have this method as you can see from the error message. This is so weired. Does it really work for you in combination with a rule that triggers the script?
  • and0r almost 6 years
    Could anyone else confirm that either of these code suggestions won't work on Outlook 2016 in the expected way? The check is fairly easy: Just copy the code in VBAs OutlookSession Modul and create a standard Outlook-Rule which executes this script if a message with subject "MY_SUBJECT" arrives. Then send yourself a message with this subject and see if it is deleted from inbox and from Deleted-Items. In my experience the latter does not apply. I slightly doubt myself, and a confirmation would help... Thanks a lot
  • EFH about 5 years
    Use the new userproperty added as a filter for an _ItemAdd set on the "Deleted Items" folder?