Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We have an automated system that sends updates via email. That's fine, and for me it is useful information. I'd prefer to only see the most recent update, and have the rest trashed. That is, given the following list

enter image description here

I'd only want the selected version of 34022's update to remain in my folder. I've dug around in Rules, but nothing seems to fit the bill.

Is there a way to (automatically) delete the ones I don't want?

share|improve this question

With some VBA. Always a good idea to test thoroughly when deleting is involved.

Private Sub Application_NewMail()
' In ThisOutlookSession module

' see Create Outlook Rules Programmatically

Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFld As Outlook.MAPIFolder
Dim objMail As Object

Set olApp = Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFld = olNs.GetDefaultFolder(olFolderInbox)

olFld.items.sort "Received", False
'Set objMail = olFld.items.GetFirst ' In Outlook 2003
Set objMail = olFld.items.GetLast ' In Outlook 2010

If TypeOf objMail Is MailItem Then

    If objMail.SenderEmailAddress = "Found in DetermineSenderEmailAddress" And _
        InStr(1, objMail.Subject, "Ticket:") Then

            DeleteOldStatus objMail

    End If

End If

Set objMail = Nothing
Set olFld = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub

Sub DeleteOldStatus(objMail As MailItem)

Dim olFld As folder
Dim olNs As NameSpace
Dim olderMail As MailItem

Dim iDel As Long

Set olNs = Application.GetNamespace("MAPI")
Set olFld = olNs.GetDefaultFolder(olFolderInbox)

For iDel = olFld.items.Count To 1 Step -1

    Set olderMail = olFld.items(iDel)

    If olderMail.Subject = objMail.Subject Then

        If olderMail.ReceivedTime < objMail.ReceivedTime Then

            Debug.Print olderMail.Subject & " received  " & olderMail.ReceivedTime & " should be deleted."
            'olderMail.Delete ' Remove leading apostrophe to uncomment when ready

        End If

    End If


Debug.Print "Done - " & objMail.Subject

End Sub

Sub DetermineSenderEmailAddress()

' open up an email then run

Dim currItem As MailItem
Set currItem = ActiveInspector.currentItem

' Copy the text from the immediate pane.
Debug.Print currItem.SenderEmailAddress

End Sub

Editor and button help -

Macro security should be set to medium.

Button help -

Edit: Fixed the InStr line

share|improve this answer
Thanks (I'd almost forgotten about this). It looks like we're checking the olderMail object (InStr(1, olderMail.Subject, "Ticket:")) before setting it (in DeleteOldStatus()), but this certainly gives me a starting point from which to work. – StevenV Aug 3 '12 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .