Sort emails into subfolders based on sender and deleting emails automatically

Not open for further replies.

Anthony Antoun

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Hi All,

Sorry in advance if this question has been asked already, I just can't seem to find anything online that I can understand.

My experience with VBA is limited to its applications with excel. At the moment, every time I save my spreadsheet at work, it will automatically send that saved file to my email address. This happens multiple times a day and I can't keep moving the emails into a subfolder. How can I automate Outlook so it checks incoming emails for the the sender (in this case it will be me) and then for the subject (I sometimes send my self reminder emails so I need to be able to filter them), then if it matches the criterea, move that email into a sub folder.

In addition to this, the spreadsheet file is about 5mb and it quickly eats into my allowable space in outlook. Is there a way that Outlook can check to see how many emails are in a sub folder, and if it meets a criteria it will automatically delete the oldest email? Specifically, i only want to keep the last 5 revisions in my emails so every time a new one comes in, it will delete the oldest one.

Thanks in advance for the help!

**Just a quick edit, I do not have a coding background at all and everything I know about excel has been learned through reading online guides extensively. I see a lot of code for Outlook and it is all foreign to me, if you could please explain what is happening in the code with comments that would be so greatly appreciated! Thanks again!

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This should work for you - Use a Rule to delete older messages as new ones arrive - well, it's close. It handles the deleting old mail part so you only have one copy, but you could tweak it to delete mail older than a specific age.
If objVariant.Subject = Item.Subject And objVariant.SentOn < Item.SentOn + 1 Then ' should be yesterday

This one shows how to move messages - Move messages CC'd to an address - remove the IF /End if lines and put the two scripts together in a rule

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Did not test it, so there could be errors, but something like this in a rule should do it. (Don't add other actions to run a script rules- the script needs to do all the actions.)

Another option is an itemadd macro - a rule could move the messages and itemadd watches the folder for new items then deletes old ones.

Sub MoveDeleteMail(Item As Outlook.MailItem)
    Dim strID As String
    Dim objMail As Outlook.MailItem
    Dim objInbox As Outlook.MAPIFolder
    Dim intCount As Integer
    Dim objVariant As Variant

Set objInbox = Session.GetDefaultFolder(olFolderInbox)

    strID = Item.EntryID
    Set objMail = Application.Session.GetItemFromID(strID)

    objMail.Move objInbox.Folders("subfolder-name")

For intCount = objInbox.Items.Count To 1 Step -1
Set objVariant = objInbox.Items.Item(intCount)
If objVariant.MessageClass = "IPM.Note" Then
    If objVariant.Subject = Item.Subject And objVariant.SentOn < Item.SentOn Then
    End If
End If

Set objMail = Nothing
Set objInbox  = Nothing
End Sub

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
See one error right off - its looking in the inbox, not the subfolder.

Changing these two lines should fix it -
Set objInbox = Session.GetDefaultFolder(olFolderInbox).Folders("subfolder-name")

objMail.Move objInbox

this changes the objinbox object to the subfolder.
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
A Custom VBA to sort emails into folders Outlook VBA and Custom Forms 0
D [TOOL] QuickModules to sort emails very quickly to multiple folders Outlook VBA and Custom Forms 1
P Is it possible to sort emails by COUNT of "From" field? Using Outlook 2
J Macro to periodically sort emails by flag status? Using Outlook 1
M using excel to sort outlook appointment items Outlook VBA and Custom Forms 4
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
Witzker print-list-of-outlook-folders with sort posibility Outlook VBA and Custom Forms 7
King Mustard Sort search groups by amount of items? Using Outlook 1
D Sort Problem with Sent Folders Using Outlook 1
E Outlook 2010 Subject sort uses Thread-Topic for grouping Using Outlook 2
Diane Poremsky Sort messages by Sender domain Using Outlook 1
J Can I sort meeting requests by the date of meeting? Using Outlook 4
T VBA to Sort Rules [A-Z] - code provided Outlook VBA and Custom Forms 9
L Sort bar at top of inbox preview outlook 2013 Using Outlook 1
C Outlook 2010, Need to restrict domain users from installing any sort of a addin in outlook Using Outlook 2
J How to sort incoming email that is sent to a different address to my main one? Using Outlook 1
D Contacts Sort Order Using Outlook 0
kburrows Office 365 - Contacts Sort First, Last instead of Last, First. How to Reset? Using Outlook 4
I 2013 Calendar sort date Using Outlook 3
F Not keep sort order after re-change column Using Outlook 1
J outlook having sort by date or from unable to display sender name. Using Outlook 2
2 Sort taks by folder then by date? Using Outlook 5
S Outlook 2007 Sort Column Shading Using Outlook 1
S Public FolderEmail Sort Problems Outlook 2010/Migration from 2003 SBS to 2011S Using Outlook 3
D Sort e-mail by sender's domain - Outlook 2007 Using Outlook 2
B outlook sort by Using Outlook 2
A Sorting of existing contact in Outlook 2003 just will not sort Using Outlook 2
J Outlook 2010 Inbox sort FROM column not working Using Outlook 29
S changing number sort BCM (Business Contact Manager) 1
B Macro to Change sort and collapse groups in Mail folder Outlook VBA and Custom Forms 9
L Interating throught a task list, respecting sort rules in the view Outlook VBA and Custom Forms 1
A How to Sort by Unread and then Read mail in Outlook 2007 Using Outlook 7
S How do i sort email by Domain? Outlook VBA and Custom Forms 2
K sort over flagstatus Outlook VBA and Custom Forms 4
F Color code certain INBOX emails Using Outlook 2
J gmail and deleted emails. Using Outlook 0
Z Outlook 2021 Outlook new emails notification not working Using Outlook 5
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
U Outlook not responding when trying to print Emails Using Outlook 6
K mark emails with colour manually (like in thunderbird) Using Outlook 1
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
U Outlook on the iPhone cannot approve filtered Emails Using Outlook 0
K Outlook 365 After migrating to Outlook 365, some contacts display in emails with prefixes Using Outlook 0
B Move emails from one account to another Outlook VBA and Custom Forms 2
D Unable to view older emails in desktop app Using Outlook 0
S Outlook 365 Help me create a Macro to make some received emails into tasks? Outlook VBA and Custom Forms 1
R Outlook 2021 Having problem setting up outlook 2021 with windows 11. I have 3 gmail accounts and I want the 3 gmail, emails to merge into the same outlook input. Using accounts in Outlook 0
S Outlook 2021 Can you make emails from 3 word domains "safe" by entering top 2 word domain into Safe List in Outlook? Using Outlook 1
J Outlook 365 Emails showing as links and text only Using Outlook 4

Similar threads