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
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
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
K 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
D Wrong email address in Outlook 2003 "From" tab in new outgoing emails Using Outlook 0
witzker How to find all emails from and to a contact in OL 2019 Using Outlook 6
S How to find emails that I sent that have not received a reply? Using Outlook 7
M Extract "Date sent" from emails (saved to folder using drag and drop) Outlook VBA and Custom Forms 1
R Sent emails show iCloud mail account not the alias Using Outlook 1
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
J how to stop junk emails from * ? Using Outlook 0
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S Auto forward for multiple emails Outlook VBA and Custom Forms 0
M All fonts in Outlook emails display with exaggerated character spacing Using Outlook 1
L Images not displaying in emails sent to self Using Outlook 2
C Icons (arrows) not showing on emails forwarded or replied to Using Outlook 5
R Moved 6 months worth (approx 1500 emails) lost from moving from TPG inbox to Icloud inbox (folders) Using Outlook 3
U Approving Emails from iPhone Using Outlook 0
D cannot view emails in View pane (in the right pane), I only see one line or nothing Using Outlook 21
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0

Similar threads