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.

I get two kinds of e-mail: Discussion and Notifications. Notifications are auto-generated e-mails for things like code updates, issue tracking updates, and so forth. I have filters set up that give them a "Notification" category.

I want to keep the discussions indefinitely, since you never know when you might need the info from an e-mail six months ago. But, the notifications are ephemeral, and there is no need to have them clog disk space.

Is there a way to make Outlook auto-archive/auto-delete/purge/expire/whatever these e-mails based on the category?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Okay, I just wrote my first Outlook macro - so don't blame me if it deletes all your mails. Backup before you try! :)

In Outlook go to Tools > Macros > Macro. Enter the name you want to give the macro. I named mine "Delete_Old_Notifications".

Here's the macro code I wrote:

Sub Delete_Old_Notification()
    Dim ns As Outlook.NameSpace
    Set ns = Application.GetNamespace("MAPI")

    Dim inbox As Outlook.MAPIFolder
    Set inbox = ns.GetDefaultFolder(olFolderInbox)

    Dim item As Object 'Outlook.MailItem
    Dim Action As Integer
    Dim Categories, Category

    Action = 0

    For Each item In inbox.Items
    If Len(item.Categories & "") > 0 Then
        Categories = Split(item.Categories, ";")

        For Each Category In Categories
        Select Case LCase(Trim(Category))
            Case "notification":
            If DateDiff("d", item.ReceivedTime, Now) > 14 Then
                Action = 1
            End If
        End Select
        Next
    End If

    Select Case Action
        Case 1: MsgBox ("Delete '" & item.Subject & "'") 'item.Delete
    End Select

    Action = 0
    Next
End Sub

(Could be shorter, yes - but I built it to be extensible.)

Notes:

  1. It looks in the standard inbox. If you want another folder, you have to modify the "Set inbox" folder. To look in subfolders of the inbox, add .Folders("foldername") at the end of the line
  2. Currently it checks if the mail has a category "notification". If you want another category, insert that in the Case. Just make it lower case because I convert them all to lower case - just to be sure.
  3. Currently it affects mails with an age of 14 days or above. See that in the If DateDiff line.
  4. Currently it does not actually delete, it just shows a notification. To make it actually work you need to remove the MsgBox near the bottom of the Sub and keep the line like this:

        Case 1: item.Delete
    

You might want to look at these two forum posts to find out...

share|improve this answer

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.