Sort emails into subfolders based on sender and deleting emails automatically

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.
 
Top