Or... using the Delete key to file messages.
Let me go on record as saying I think using the Delete key to file messages is a bad idea. But I know there are a lot of people doing it. A lot less since the introduction of Quick Steps, but still too many use the Delete key as a way to quickly clear out the Inbox.
Create a Quick Step
My recommended method with Outlook 2010 and 2013: create a quick step (and assign it a shortcut) to move stuff you want to keep and only use the Deleted Items folder for stuff you don't need to keep.
Once you get in the habit, Ctrl+Shift+1 can be second nature, although it is a little difficult for smaller hands to manage one-handed. If your mouse or keyboard supports programmable keys you might be able to reprogram a key for the shortcut.
Macro to move Deleted Items
This is for a user who uses the Delete key to file messages but her company cleans up the mailboxes nightly, emptying the Deleted items folder.
The macro moves all mail dropped in the deleted items folder to a folder under the Inbox.
Note: with this macro active, the only way to delete anything is by using Shift+Delete to permanently delete. It could be tweaked to look for messages meeting certain conditions, such as marked complete or assigned a category, and move (or not move) those items.
Private WithEvents Items As Outlook.Items Private Sub Application_Startup() Dim NS As Outlook.NameSpace Set NS = Application.GetNamespace("MAPI") Set Items = NS.GetDefaultFolder(olFolderDeletedItems).Items Set NS = Nothing End Sub Private Sub Items_ItemAdd(ByVal Item As Object) If TypeOf Item Is Outlook.MailItem Then Set fldMove = Session.GetDefaultFolder(olFolderInbox). _ Folders("Deleted Stuff") Item.Move fldMove End If End Sub
Move messages using a Toolbar button
Although not as easy as the Del key, you can use a toolbar button to move messages to an archive folder. In versions of Outlook that use the traditional toolbar, you can assign a keyboard shortcut. In ribbonized versions, you can add the macro to the QAT and use Alt+ the number representing its position on the QAT.
This sample moves messages from the Inbox to a subfolder named "completed".
To use this macro, you need the GetCurrentItem function at Work with Open Item or Selected Item
Sub MoveDeleted() Dim objOutlook As Outlook.Application Dim objNamespace As Outlook.NameSpace Dim objSourceFolder As Outlook.MAPIFolder Dim objDestFolder As Outlook.MAPIFolder Dim objItem As MailItem Set objOutlook = Application Set objNamespace = objOutlook.GetNamespace("MAPI") Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox) ' Get GetCurrentItem function athttp://slipstick.me/e8mio Set objItem = GetCurrentItem() Set objDestFolder = objNamespace.GetDefaultFolder(olFolderInbox).Folders("completed") objItem.Move objDestFolder Set objDestFolder = Nothing End Sub
How to use the macro
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.
To use the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
Application_Startup macros run when Outlook starts. If you are using an Application_Startup macro you can test the macro without restarting Outlook by clicking in the first line of the Application_Startup macro then clicking the Run button on the toolbar or pressing F8.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
I can not get the first macro to do anything, I did all the steps below that are mentioned. I am a very green VBA user, but have practiced some basic ones and they worked. I am using a subfolder to the inbox, is that the issue?
Do you get any error messages?
This line tells the macro to watch the Deleted items folder for new items:
Set Items = NS.GetDefaultFolder(olFolderDeletedItems).Items
This line tells it to move the deleted items to a subfolder called 'deleted stufff' under the inbox. If your folder is named something else, you'll need to use that name in the macro but otherwise, it sounds like the macro should be working.
Set fldMove = Session.GetDefaultFolder(olFolderInbox).Folders("Deleted Stuff")
it is an auto-start macro and will only run if you rrestart outlook or click Run when the cursor is in the application start macro.
I get error in VBA: "Variable not defined" with focus to "fldMove ="
How to declare "fldMove"?
With "Dim fldMove As Outlook.MAPIFolder" it was not possible: Error: '-2147221233 (8004010f)'
Try just Dim fldMove - or remove option explicit from the top of the module.
Does the folder you are using exist?
all free variants are ending with error:
"Dim fldMove As MAPIFolder"
"Dim fldMove As Outlook.MAPIFolder"
"Dim fldMove" end wirh error
The folder (as in example "Deleted Stuff") is existing in IMAP storage and is on the same level as deleted items folder.
Deactivate "Option Explicit" don't help.
Is the code compatible with Outlook 2007?
But here is second problem too:
Here is an IMAP4 account and if I press DEL-Key Outlook 2007 set all selected emails on the server as deleted without to move them to trash folder and my setting is after switching between the folders to deleted them but I wont that all this items are moving to trash folder ("soft deleted") and not to be "hard" deleted. Is it possible to moved deleted items automatically to a specified folder (after "DEL"-Key of course, not via separated macro buttons)?
How are you calling the folder? This gets you the folder at the same level as inbox, calendar, deleted:
Set fldMove = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Deleted Stuff")
Remove parent. for a subfolder.
>> I press DEL-Key Outlook 2007 set all selected emails on the server as deleted without to move them to trash folder
This is standard behavior for the older versions of outlook. Items are marked as deleted (and stay 'in place') then are purged when you change folders or initiate a purge. More info: https://www.slipstick.com/outlook/config/configure-and-use-imap-accounts/#deleted
i wrote above taht I make the folder "Deleted Stuff" in an IMAP storage and thans was wrong. Now is the folder "Deleted Stuff" in the local storage with the default folder "olFolderDeletedItems" where I move the deled items (and on the same level with him) and it work! Still a problem is available on this macro: If a moved the items via "Drag&Drop" from the folder "Deleted Stuff" in to the folder "olFolderDeletedItems" then there is no problem with the processing in VBA and the macro continues to work without to restart Outlook. But if I try to moved the items via DEL-key ("soft deleted") I get error from debugger in VBA and the macro stopped to work and I have each time to restart Outlook (or to refresh "Private Sub Application_Startup()" with F5-key). All items that would be deled within a local storage are moved automatically to this default folder "olFolderDeletedItems". But If I deleted an item within an IMAP storage (it is an IMAP4 account) the items are don’t move to this "olFolderDeletedItems" in local storage that is setting as "default deleted folder" in Outlook. I now about this limitation in Outlook 2007 and as you described… Read more »
>> But if I try to moved the items via DEL-key ("soft deleted") I get error from debugger in VBA and the macro stopped to work and I have each time to restart Outlook
what line does it stop on? It's not watching for deleted items, it's watching for messages moved into the deleted items folder (or whatever folder you identify), and shouldn't be erroring just doing other things.
>> All items that would be deled within a local storage are moved automatically to this default folder "olFolderDeletedItems". But If I deleted an item within an IMAP storage (it is an IMAP4 account) the items are don’t move to this "olFolderDeletedItems" in local storage that is setting as "default deleted folder" in Outlook.
IMAP is funky in that deleted items stay in their folder (usually hidden by a view) so the macro wouldn't work... it can watch imap folders, if you just want to move everything to a specified folder using a toolbar button.
AFAIK, you cannot map the deleted key. If i recall correctly, you can set keyboard shortcuts for buttons in Outlook 2007, so you could add the macro to the toolbar then set a shortcut for it.
Any tips on moving other deleted items like olAppointment, olContact, and olReport that experience the odd behavior of creating a new mail item when moved from the Deleted Items folder?
You'd need to use if statements and if you wanted to keep them, move them to calendar or contact folders.
If TypeOf Item Is Outlook.AppointmentItem Then
' move to calendar folder
I am new to macros so I am trying to understand/learn if the Macro to move Deleted Items will do this automatically every time outlook is opened or does it just put a shortcut on the ribbon? I would like to be able to automatically move my deleted items to a folder to share with other office staff.
The first macro is an itemadd macro and will run automatically. The second macro is used manually.
I pasted the first macro you have above and put it into the VBA module and I cant seem to get it to work. I also created a folder using the same name Deleted Stuff. Is there something else that needs to be changed in the Macro? also highlighted in red is Private WithEvents Items As Outlook.Items
Did you put the macros in ThisOutlookSession? If you put it in a module, that's why the line is red.
I have the Move Deleted Items macro code pasted into ThisOutlookSession 'VbaProject.OTM window, but it still won't run. When I press the Run button the Macros window opens with an empty list and the Run button greyed out.
the first macro is an itemadd macro - it goes in ThisOutlookSession. The second macro should be in a new module (it should work in thisoutlooksession, but it's good form to put it in a module). You will need this function to use it https://www.slipstick.com/developer/outlook-vba-work-with-open-item-or-select-item/#getcurrentitem (but that would only prevent it from working - you'd still see the macro.)
Oh... did you change the macro security? If it disabled the macros, they won't show up.
I have it set up as per this screen shot. As you can see, still no macro listed to run.
Use the Step In button on the Edit toolbar (or F8) and watch it execute each line. There aren't any on error resume next lines so it shouldn't be skipping any lines.
Where is the folder you are moving messages to? This is a folder at the same level as the inbox:
Set objDestFolder = objNamespace.GetDefaultFolder(olFolderInbox).Folders("completed")
https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/ shows how to select other folders.
I restarted my PC for other reasons this morning and now it is working :)
So some sort of restart was required - not sure if it was Outlook or Windows that was required.
Although no macro still shows up in the list when you press the green triangle, the code is working.
I did discover that I had to create the "Trash" (my name choice) folder as a sub-folder of the Inbox. It couldn't be a folder at the same level - I assume because of the path you have created within the code.
Thanks for you help Diane.
I started noticing last winter that Outlook was funky if updates were waiting for a reboot. The first incident was similar to yours - the profile was apparently corrupt. But a new profile failed to. Then i noticed the updates waiting for a reboot. I've seen other errors since them.
Yeah, the path is set in the code - you could change it to use any folder.
Automatic macros or ones used in a rule (or any others with the name in this format: 'sub subname(item as mailitem or something)') aren't visible in the Macro list on the developer tab because you can't run them manually.