vbaoutlookmessage

Permanently Delete MailMessage in Outlook with VBA?


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?


Solution

  • Try moving it first then deleting it (works on some patches 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, then loop through the deleted items folder, find it, and then call delete a second time. Mark it using a user property.

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

    loop through your deleted items look for that user property

     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