Home > PC, Software > Outlook Macro to move email to Deleted Items folder

Outlook Macro to move email to Deleted Items folder

January 16th, 2008

Updated:  Well this didn’t work out as expected (or at all half the time).  I’ll take another crack at the code when I have some time over the weekend.

I’ve been trying to use IMAP on Outlook 2007 for some time now and have been a little frustrated with the whole “purge” routine with IMAP in deleting folders.

This evening I wrote a little macro (well, cobbled together some code from around the internet) that lets me create a toolbar button that moves e-mail to the Deleted Items folder in its entirety instead of “marking it for deletion”

This lets me treat deleted items like normal Trash even in my IMAP account.

If this makes no sense to you, you’re probably not having the same issue as me and couldn’t care less 🙂

Here’s the code:

Sub MoveSelectedMessagesToDeletedItemsFolder()

On Error Resume Next
Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem

Set objNS = Application.GetNamespace(“MAPI”)
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
‘Set objFolder = objInbox.Folders(“Trash”)
Set objFolder = objNS.GetDefaultFolder(olFolderDeletedItems)
‘Assume this is a mail folder
If objFolder Is Nothing Then
MsgBox “This folder doesn’t exist!”, vbOKOnly + vbExclamation, “INVALID FOLDER”
End If

If Application.ActiveExplorer.Selection.Count = 0 Then
‘Require that this procedure be called only when a message is selected
Exit Sub
End If

For Each objItem In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move objFolder
End If
End If

Set objItem = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing

End Sub

Categories: PC, Software Tags: