Monitor secondary mailbox folder for new messages

Last reviewed on December 30, 2013

Use this code sample (along with the GetFolderPath function from Working with VBA and non-default Outlook Folders) to monitor a secondary Exchange mailbox that is open in a profile.

This code should also work to monitor any folder in another data file for new messages (not limited to Exchange accounts).

To use this code, macro security needs to be set to allow all macros or you will need to sign the code.

Paste the code into ThisOutlookSession, change the folder path you are monitoring (don't forget the GetFolderPath function) then click in the Application_Startup macro and click Run.



Option Explicit
 Private objNS As Outlook.NameSpace
 Private WithEvents objNewMailItems As Outlook.Items
 
Private Sub Application_Startup()

 Dim objMyInbox As Outlook.MAPIFolder

' Use GetFolderPath function from
' http://slipstick.me/qf
Set objNewMailItems = GetFolderPath("Secondary Mailbox Name\Inbox").Items 
 Set objMyInbox = Nothing
 End Sub


 Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)

 'Ensure we are only working with e-mail items
 'If Item.Class <> OlItemType.olMailItem Then Exit Sub

 MsgBox "Message subject: " & Item.Subject & " received in Second Mailbox Name  mailbox", vbCritical

 End Sub 


Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Please post long or more complicated questions at Outlookforums.

Leave a Reply

If the Post Coment button disappears, press your Tab key.