Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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
' http://msdn.microsoft.com/en-us/library/aa163981(v=office.10).aspx

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

Next

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 - http://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/

Macro security should be set to medium.

Button help - http://www.howto-outlook.com/howto/macrobutton.htm

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.