Outlook Move Old Emails | VBA Macro #7

Share on facebook
Share on twitter
Share on linkedin

Macro Intro

This Outlook macro makes cleaning out your inbox as easy as one click. The Move Old Emails macro will automatically move emails out of your inbox and into a different folder. Emails only move if they qualify for the criteria you set. (Example: The email is Read, has no Flags Marked, and No Categories set) No longer will you have to manually move old emails out of your inbox and make mistakes dragging emails to different folders. The Move Old Emails macro will do this for you allowing you to focus on what emails you have left. Spend more time in your inbox and get more done! This macro is easily customizable to meet your unique needs. ***Tip: The Move Old Emails macro can be scheduled to reoccur automatically on your Outlook Calendar using the Call action from the Outlook Calendar Reminder Macros.

See it in Action

Watch this video to see this macro in action.

Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Outlook 16.0 Object Library
***Recent Code Updates
12.18.2021 Update to use the Sort method allowing the loop through the StartFolder to start with the most recent received email (StartFolderItems.Sort “[ReceivedTime]”, True) or the oldest received email (StartFolderItems.Sort “[ReceivedTime]”, False). Update what Email Object properties are readily available to interact with as criteria to determine if an email qualifies to move to the EndFolder. By Default the EndFolder will be the Deleted Items Folder unless indicated otherwise.
12.26.2021 After further testing of 3 different ways to loop through an Outlook folder only one was looping through all emails without a Run Time Error ‘440’; Array index out of bounds error. (Do Until CurrentEmail > StartFolderItems.Count Loop & For Each Email in StartFolderItems NOT WORKING) Added For CurrentEmail = StartFolderItems.Count To 1 Step -1 works through all items in an Outlook folder and no counters (CurrentEmail & LastEmail) needed. Starting from the oldest or most recent email is determined by how the Descending parameter is set in the Sort method. If False start with the most recent. If True start with the oldest. By Default, this parameter is set to False.
Copy the Code
'Leverage & Lean "Less Clicks, More Results"
Sub MoveOldEmailsFreeMacro()
' Means variable is in use
Dim Email As Object '
Dim EndFolder As Outlook.MAPIFolder '
Dim OutlookApp As Outlook.Application '
Dim OutlookNS As Outlook.NameSpace '
Dim StartFolder As Outlook.MAPIFolder '
Dim StartFolderItems As Outlook.Items '
 
On Error GoTo LeverageLean
 
Set OutlookApp = Outlook.Application
Set OutlookNS = OutlookApp.GetNamespace("MAPI")
Set StartFolder = OutlookNS.GetDefaultFolder(olFolderInbox) 'Default Outlook Account's Inbox Folder
'Set StartFolder = OutlookNS.Folders("ACCOUNT NAME").Folders("Inbox") 'Different Outlook Account's Inbox Folder (Ex. "brentschneider@leveragelean.com")
'Set StartFolder = StartFolder.Folders("SubFolder Name") 'Update the Start Folder to look at a Sub Folder under the Inbox Folder.
Set EndFolder = OutlookNS.GetDefaultFolder(olFolderDeletedItems) 'Default Outlook Account's Deleted Items Folder
'Set EndFolder = OutlookNS.Folders("ACCOUNT NAME").Folders("Deleted Items") 'Different Outlook Account's Deleted Items Folder (Ex. "brentschneider@leveragelean.com")
'Set EndFolder = EndFolder.Folders("SubFolder Name") 'Update the End Folder to look at a Sub Folder under the Deleted Items Folder.

Set StartFolderItems = StartFolder.Items
StartFolderItems.Sort "[ReceivedTime]", False 'Sort the StartfolderItems based on the Item's Received Time. Starting from the oldest or most recent email is determined by how the Descending parameter is set. If True start with the oldest. If False start with the most recent.

For CurrentEmail = StartFolderItems.Count To 1 Step -1
Set Email = StartFolderItems(CurrentEmail)
If Email.UnRead = False And Email.FlagStatus <> 2 And Email.Categories = "" Then 'Set If Statement with necessary criteria using Email properties and values
Email.Move EndFolder 'If Email is Read, has no Flag Marked, and no Category set. Move the End Folder
End If
Next

Set Email = Nothing
Set EndFolder = Nothing
Set OutlookApp = Nothing
Set OutlookNS = Nothing
Set StartFolder = Nothing
Set StartFolderItems = Nothing

Exit Sub

LeverageLean:
MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")
 
End Sub
'Stay Awesome

Insider Content

Here is the Insider code for this macro. With this macro you can use an Excel Workbook to easily loop through multiple Outlook email folders and use unique criteria for each folder to determine when emails should be moved. Simply create a Worksheet per Outlook email folder you want to loop through. The Worksheet Name is used to determine folder name so they must match. Within each Worksheet list out criteria that matters for each folder. If you want certain email criteria to be ignored make it a comment directly in the macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Outlook 16.0 Object Library, Microsoft Office 16.0 Object Library, Microsoft Excel 16.0 Object Library

Customization

These segments of code can be customized to personalize this macro.
The StartFolder variable is used to determine which Outlook folder to loop through for emails to move. By Default the Start Folder is setup to be the default Outlook Account’s Inbox. You can adjust the Start Folder to be a different Outlook Account and even a different Outlook Folder. (See additional customization for more details)
This code line can be used to change the StartFolder variable to a different Outlook Account. Simply update “ACCOUNT NAME” with the Outlook Account’s Email Address. (Ex. “brentschneider@leveragelean.com”) By Default this Outlook Account will be looking to the Inbox.
This code line can be used to change the StartFolder variable to an Outlook Sub Folder underneath the Inbox. Simply update “SubFolder Name” with the Outlook Folder’s Name.
The EndFolder variable is used to determine which Outlook folder emails (that meet criteria) are sent too. By Default the End Folder is setup to be the default Outlook Account’s Deleted Items folder. You can adjust the End Folder to be a different Outlook Account and even a different Outlook Folder. (See additional customization for more details)
This code line can be used to change the EndFolder variable to a different Outlook Account. Simply update “ACCOUNT NAME” with the Outlook Account’s Email Address. (Ex. “brentschneider@leveragelean.com”) By Default this Outlook Account will be looking to the Deleted Items Folder.
This code line can be used to change the EndFolder variable to an Outlook Sub Folder. Simply update “SubFolder Name” with the Outlook Folder’s Name.
By Default this macro will start with the most recent email when looping through an Outlook folder. This behavior is controlled by the Sort method referencing the [ReceivedTime] property. Starting from the oldest or most recent email is determined by how the Descending parameter is set. If True start with the oldest. If False start with the most recent. To learn more about the Sort method look here!
This If Then Statement is used to determine if an email should be moved from the Start Folder to the End Folder. That determination is made by using the Outlook MailItem Object Properties to evaluate each email with specific criteria. Currently this macro is setup to only look for emails that are Read, have no Flag Status, and no Category set. To see more Outlook MailItem Properties available navigate here!

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Access, Excel, Outlook, PowerPoint, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Powered By MemberPress WooCommerce Plus Integration

Free Macro

Start creating Less Clicks, More Results today!