Macro to permanently delete message

J

John Robison

Using the text:

For Each myItem In Application.ActiveExplorer.Selection
myItem.Delete

I am able to create a macro to delete an Outlook item. However, the item
goes to the deleted items folder and I would like it to bypass that folder.
Can this code be modified to permanently delete the message?

Thanks,

John
 
K

Ken Slovak - [MVP - Outlook]

You would need to use CDO code to do that. Outlook's Delete moves the
item to the Deleted Items folder. CDO's Delete bypasses Deleted Items.
CDO 1.21 (Collaboration Data Objects) is an optional installation with
Office 2000 and later.

Also, when deleting items from a collection never ever use a For Each
loop or a count-up For loop. You are messing with the loop counter and
it gets confused and you only end up deleting every other item for
each pass through the loop. Use a count-down For loop instead:

'This code would require that CDO.DLL is installed and
'referenced in your VBA project

Dim oCDO As MAPI.Session
Dim oMessage As MAPI.Message
Dim sEntryID As String
Dim sStoreID As String
Dim i As Long

Set oCDO = CreateObject("MAPI.Session")
oCDO.Logon "", "", False, False

For i = Application.ActiveExplorer.Selection.Count To 1 Step -1
Set myItem = Application.ActiveExplorer.Selection.Item(i)
sEntryID = myItem.EntryID
sStoreID = myItem.Parent.StoreId
Set oMessage = oCDO.GetMessage(sEntryID, sStoreID)
oMessage.Delete
Next i
 
J

John Robison

Thanks Ken! I appreciate the help. This is good stuff!!!

So is the CDO code the method that is used when a user holds down "Shift"
with "delete"?

John
 
K

Ken Slovak - [MVP - Outlook]

Using CDO or Extended MAPI to delete an item is the equivalent of a
hard delete using Shift+Delete. I'm not sure what's going on under the
hood but I assume the equivalent Extended MAPI method is being used,
Outlook uses Extended MAPI for its coding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top