Sort emails into subfolders based on sender and deleting emails automatically

Status
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!
 
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
 
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.

Code:
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
     objVariant.Delete
     Else
    End If
End If
Next

Set objMail = Nothing
Set objInbox  = Nothing
End Sub
 
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.
 
Status
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
H Move Selected emails to Local Drive Outlook VBA and Custom Forms 0
D Delete Outlook emails from MS server Using Outlook 12
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
A Flagged Emails highlighted in yellow Using Outlook 2
P Search folder: all emails sent to or from a domain Using Outlook 1
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
G Save emails as msg file from Outlook Web AddIn (Office JS) Outlook VBA and Custom Forms 0
T Outlook 2010 Sub accounts not showing new emails in Inbox Using Outlook 4
Nufc1980 Outlook "Please treat this as private label" auto added to some emails - Help. Using Outlook 3
humility36 Cannot move emails to archive - 440 error Outlook VBA and Custom Forms 1
E Edit incoming emails to remove a certain sentence added by the "system" Using Outlook 1
R Saving Emails and Attachments as .msg file Using Outlook 3
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 4

Similar threads

Back
Top