Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I receive autonomous messages via email that provide me a dashboard view of the status of various systems in my company. I want to have outlook automatically move the newest status message to a specified folder and then delete the previous messages in that folder. What is the best way to do this?

share|improve this question

When there is new mail in the inbox this programatic rule finds the folder, deletes the contents, then moves the new mail into it.

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

' GetFirst used to work in Outlook 2003 now it is GetLast in Outlook 2010   
Set objMail = olFld.items.GetLast

If TypeOf objMail Is MailItem Then
    DeleteBeforeNewStatus objMail
End If

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

End Sub

Sub DeleteBeforeNewStatus(objMail As MailItem)
' In any module 

Dim olFld As folder
Dim olNs As NameSpace

Dim olderMail As MailItem
Dim iDel As Long

Set olNs = Application.GetNamespace("MAPI")

Select Case objMail.Subject

    Case "System A Status"
        Set olFld = olNs.GetDefaultFolder(olFolderInbox).Folders("SystemA")

    Case "System B Status"
        Set olFld = olNs.GetDefaultFolder(olFolderInbox).Folders("SystemB")

    Case Else
        Set olNs = Nothing
        Exit Sub

End Select

For iDel = olFld.items.Count To 1 Step -1
    Set olderMail = olFld.items(iDel)

 objMail.Move olFld

End Sub

Create two test folders directly under the inbox to test. If it works add as many instances of these two lines as required.

 Case "System X Status"
      Set olFld = olNs.GetDefaultFolder(olFolderInbox).Folders("SystemX")

If the folders are buried deeper you will have to include the path in this format.

Set olFld = olNs.GetDefaultFolder(olFolderInbox).Folders("Subfolder1").Folders(Subfolder2").Folders("SystemX")

Editor and button help -

Macro security should be set to medium.

Button help -

share|improve this answer

Your Answer


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.