12

I am looking for a way to permanently delete a MailMessage from Outlook 2000 with VBA code. I'd like to do this without having to do a second loop to empty the Deleted items.

Essentially, I am looking for a code equivalent to the UI method of clicking a message and hitting SHIFT+DELETE.

Is there such a thing?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
eidylon
  • 7,068
  • 20
  • 75
  • 118

5 Answers5

14

Try moving it first then deleting it (works on some patchs in 2000) or use RDO or CDO to do the job for you (you will have to install them)

  Set objDeletedItem = objDeletedItem.Move(DeletedFolder)
  objDeletedItem.Delete

CDO way

Set objCDOSession = CreateObject("MAPI.Session")
objCDOSession.Logon "", "", False, False
Set objMail = objCDOSession.GetMessage(objItem.EntryID, objItem.Parent.StoreID)
objMail.Delete

RDO

set objRDOSession = CreateObject("Redemption.RDOSession")
objRDOSession.MAPIOBJECT = objItem.Session.MAPIOBJECT 
set objMail = objRDOSession.GetMessageFromID(objItem.EntryID>)
objMail.Delete

You could also mark the message first before you delete it and the loop through the deleted items folder and find it an dthe call delete a second time. Mark it using a Userproperty.

objMail.UserProperties.Add "Deleted", olText
objMail.Save
objMail.Delete

loop through you deleted items look for that userprop

 Set objDeletedFolder = myNameSpace.GetDefaultFolder(olFolderDeletedItems)
    For Each objItem In objDeletedFolder.Items
        Set objProperty = objItem.UserProperties.Find("Deleted")
        If TypeName(objProperty) <> "Nothing" Then
            objItem.Delete
        End If
    Next
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
76mel
  • 3,993
  • 2
  • 22
  • 21
  • I'd rather not go CDO or RDO, cuz my whole point here was to reduce code complexity, and I'd rather not go introducing all new dependencies. The second is basicaly what I am doing now. I first do (pseudo): for each msg in inbox msg.delete '// moves to deleted items next for each msg in deletedItems msg.delete '// deletes permanently next whereas what i'd like to do, is something like: for each msg in inbox msg.delete(permanent = true) next – eidylon Jul 10 '09 at 18:16
  • There is no such method in 2000. You will have to create your own function in VBA. What is wrong with the move-delete or your loop ? is it performance ? – 76mel Jul 10 '09 at 19:18
  • Nothing inherently "wrong" with it, i was just looking for a way to do it more cleanly than relying on two loops. Thanks anyway though. – eidylon Jul 14 '09 at 00:24
  • For RDO, you can replace the line objRDOSession.Logon with objRDOSession.MAPIOBJECT = Application.Sessionn.MAPIOBJECT – Dmitry Streblechenko Mar 04 '14 at 22:26
2

I know this is an old thread, but since I recently had cause to write a macro that does this, I thought I'd share. I found that the Remove method appears to be a permanent deletion. I'm using this snippet:

While oFilteredItems.Count > 0
    Debug.Print "   " & oFilteredItems.GetFirst.Subject
    oFilteredItems.Remove 1
Wend

I begin with a list of items that have been filtered by some criteria. Then, I just delete one at a time until it's gone.

HTH

end-user
  • 2,845
  • 6
  • 30
  • 56
2

Simplest solution of all, similar to the first way:

  FindID = deleteme.EntryID
  deleteme.Delete
  set deleteme = NameSpace.GetItemFromID(FindID)
  deleteme.Delete

Do it twice and it'll be gone for good, and no performance killing loop. (NameSpace can be a particular namespace variable, if not in the default store.) Note this only works if you don't delete across stores, which can change the EntryID or remove it entirely.

SilverbackNet
  • 2,076
  • 17
  • 29
1

You can use the following approach, basically you delete all of your email messages as you are currently doing, then call this one line to empty the deleted items folder. The code is in jscript, but I can translate if you really need me to :)

var app = GetObject("", "Outlook.Application"); //use new ActiveXObject if fails

app.ActiveExplorer().CommandBars("Menu Bar").Controls("Tools").Controls('Empty "Deleted Items" Folder').Execute();
Marcus Pope
  • 2,293
  • 20
  • 25
1

Recently I had to permamentnly delete all contacts. This worked for me (Outlook 2016). You have obtain new reference to the item in the trash folder, otherwise it says "already deleted" or something like that. Just go from the end and the recently moved items will be there. Then calling Delete achieves permanent deletion. This snippet can be used in a loop.

    myContacts(i).Move (trashFolder)
    trashCount = trashFolder.Items.Count
    For j = trashCount To 1 Step -1
        Set trashItem = trashFolder.Items(j)
        If trashItem.MessageClass = "IPM.Contact" Then
            trashItem.Delete
        Else
            Exit For
        End If
    Next
Mr. TA
  • 5,230
  • 1
  • 28
  • 35