I want to delete an older email when a new message comes in (they have the same subject line).
This run a script sample is perfect to use with status reports and similar messages where you really only need to keep the most recent copy. For it to work, the messages need to have the same subject line (and the subject should be unique, because all messages matching the condition will be deleted.)
To use, create a Rule that checks the message subject and choose Run a Script as the action, selecting this script. The macro checks the Item.Subject, so if you need to run it on messages with different subjects you can use one rule.
While it only runs if Outlook is open, old messages won't pile up, as it deletes all older messages that meet the conditions when Outlook is open.
See Outlook's Rules and Alerts: Run a Script for more information on using Run A Script rules.
Sub DeleteOlderMessages(Item As Outlook.MailItem) Dim objInbox As Outlook.MAPIFolder Dim intCount As Integer Dim objVariant As Variant Set objInbox = Session.GetDefaultFolder(olFolderInbox) ' Remove these lines if you don't want to add a category Item.Categories = "Delete Older" Item.Save 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 objInbox = Nothing End Sub
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
More Run a Script Samples:
- Autoaccept a Meeting Request using Rules
- Automatically Add a Category to Accepted Meetings
- Convert RTF Messages to Plain Text Format
- Create a rule to delete mail after a number of days
- Delegates, Meeting Requests, and Rules
- Forward meeting details to another address
- How to Change the Font used for Outlook's RSS Feeds
- Keep Canceled Meetings on Outlook's Calendar
- Macro to Print Outlook email attachments as they arrive
- Move messages CC'd to an address
- Outlook AutoReplies: One script, many responses
- Outlook's Rules and Alerts: Run a Script
- Process messages received on a day of the week
- Read Outlook Messages using Plain Text
- Receive a Reminder When a Message Doesn't Arrive?
- Run a script rule: Autoreply using a template
- Run a Script Rule: Change Subject then Forward Message
- Run a script rule: Reply to a message
- Run a Script Rule: Send a new message when a message arrives
- Run Rules Now using a Macro
- Save all incoming messages to the hard drive
- Save and Rename Outlook Email Attachments
- Save Outlook email as a PDF
- Sort messages by Sender domain
- Use a macro to copy data in Outlook email to Excel workbook
- Use a run a script rule to mark messages read
- Use VBA to move messages with attachments