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 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

1 Answer 1

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
' 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

' 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 - 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

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.