Outlook Macro to move email to Deleted Items folder
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.MailItemSet 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 IfIf Application.ActiveExplorer.Selection.Count = 0 Then
‘Require that this procedure be called only when a message is selected
Exit Sub
End IfFor Each objItem In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move objFolder
End If
End If
NextSet objItem = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = NothingEnd Sub
I was looking to do the same. There is nothing out there that does this exactly right so I put together something that does work mashing together a few scripts that are close:
' MoveAndExpungeIMAP
' Created by Henry Weismann Using other Scripts
' info@weismannweb.com
'
'
' this code may be used in compiled form in any way you desire. this
' file may be redistributed unmodified by any means PROVIDING it is
' not sold for profit without the authors written consent, and
' providing that this notice and the authors name is included. If
' the source code in this file is used in any commercial application
' then acknowledgement must be made to the author of this file
' (in whatever form you wish).
'
' this file is provided "as is" with no expressed or implied warranty.
' the author accepts no liability for any damage caused through use.
'
' important: expect bugs.
'
' use and enjoy. :-)
Sub MoveSelectedMessagesToFolder()
On Error Resume Next
Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
Dim barEdit, btnPurge, btnConnect, thisFolder, accountFolder
Dim myExplorer As Explorer
Set myExplorer = Application.ActiveExplorer
Set objNS = Application.GetNamespace("MAPI")
'Locate root folder for this account
'Set thisFolder = myExplorer.currentFolder
'Do Until thisFolder.Parent = objNS
' Set thisFolder = thisFolder.Parent
'Loop
'Set accountFolder = thisFolder
'Set objFolder = accountFolder.Folders("Trash")
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
Next
Set barEdit = ActiveExplorer.CommandBars("Edit")
Set btnPurge = barEdit.FindControl(msoControlButton, 5583, , , True)
Set barEdit = ActiveExplorer.CommandBars("File")
Set btnConnect = barEdit.FindControl(msoControlButton, 9441, , , True)
If btnPurge Is Nothing Or btnConnect Is Nothing Then
MsgBox "Select a valid IMAP folder once at least, then run this macro again.", vbInformation, "MatroExpungeIMAP"
Exit Sub
End If
If objNS.Offline Then
MsgBox "Go Online, then run this macro again.", vbInformation, "MatroExpungeIMAP"
Exit Sub
End If
Call ExpungeCurrentFolder(objInbox, btnPurge, btnConnect)
Set objItem = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
End Sub
Private Sub ExpungeCurrentFolder(folder, btnPurge, btnConnect)
DoEvents
If folder.DefaultItemType = olMailItem And Not btnConnect.Enabled Then
If btnPurge.Visible Then
btnPurge.Execute
End If
End If
End Sub
Let me know if it works
[…] Link: Move and Expunge IMAP code […]
I might be mistaken, but it looks like your code is more complicated than it needs to be. You can simply call the .Delete method of the MailItem to send items to the deleted items folder. There would be no need for the objFolder reference at all.
For Each objItem In Application.ActiveExplorer.Selection
If TypeName(objItem) = “MailItem” Then
objItem.Delete
End If
Next objItem
Also you should declare objItem as ‘Object’ because, in the Inbox, you can select items other than messages.
HTH,
JP
hi friends,
I am looking for a feature as below Microsoft Office 2003:
When I will press a “Delete” for a mail item, by default it goes to ‘Deleted Items” folder. But I would like to move this item to a different folder other than “Deleted item”.
The intention of this is : I dont want to delete any item. Rather would like to move to a archive folder.
Can anybody tell me how to configure (either through some MACRO) in Outlook 2003 ?
Thanks in Advance.
Debesh