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!
 

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.

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
 

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.
 
Status
Not open for further replies.
Thread starter Similar threads 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
G Set the default sort order for emails Using Outlook 4
T Outlook 2010 will not sort emails - XP Using Outlook 1
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 New Slipstick.com Articles 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
L When I send multiple BCC e-mails I usually get an error of the sort: 5.1.2 - Bad destination host "D Using Outlook 2
B Contacts not sorting in "File As" sort order Using Outlook 3
A Cannot sort sent items, always displays by 'From' Using Outlook 1
M Strange sorting in sort by day Using Outlook 2
A Sorting of existing contact in Outlook 2003 just will not sort Using Outlook 2
C Public Folders, Email Lists, and Sort Orders Using Outlook 2
L Outlook autocomplete sort order Using Outlook 3
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
M Sync 'On my computer' emails Using Outlook 1
A Apply Selected Emails to outlook rules and Run Rules Using Outlook 5
A Run-time error '430' on certain emails when trying to set "Outlook.mailitem" as "ActiveExplorer.Selection.Item" Outlook VBA and Custom Forms 2
J Moved many emails to Outlook external folder, need to delete on Gmail server Using Outlook 14
P Move emails between 2 mailboxes. Using Outlook 0
R Assign Categories "Round Robin" style but in a shared mailbox but on specific emails only Outlook VBA and Custom Forms 8
P Outlook for Mac - emails are not sent out beginning last week Using Outlook 1
M Missing emails in Inbox, but are present in webmail Using Outlook 0
A How To Get Body of All Emails to view in Blue Using Outlook 1
A How to get body of all emails in outlook 2016 to view in blue color Using Outlook 1
N Extract Outlook emails to excel Outlook VBA and Custom Forms 2
Similar threads


















































Top