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() ' Use GetFolderPath function from 'http://slipstick.me/qf Set objNewMailItems = GetFolderPath("Secondary Mailbox Name\Inbox").Items 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
Good day to you, hope you are well.
I have a shared mailbox that has multiple unread emails accross multiple non default folders.
Can this code be amended to run a macro that marks all unread items as read.
HI
would it be possible to run different macro for each item.add in inbox subfolders?
Yes, you need to identify the folder in the application startup macro then duplicate the itemadd macro, using the name assigned to the path.
Set objSubfolderItems = GetFolderPath("Secondary Mailbox Name\Inbox\subfolder").Items
End Sub
Private Sub objSubfolderItems_ItemAdd(ByVal Item As Object)
Thank you
I have set up several monitors and noticed some e-mails wasnt processed.
It looks like when one macro runs and item_add occure in other folder it doesnt queue up.
Not sure if my observation is right but if so is there a way to make sure every item processed.
As long as they don't come in fast and furious or the macro doesn't take a long time to run, it should get all messages. Try moving most of the working macro into a new one - so itemadd watches and picks up the message then hands it off to another macro for processing.
Diane,
thank you, that works great. However Item.Class is 43 for an email and OlItemType.olMailItem is 0 in Outlook 2010.
So I changed
If Item.Class OlItemType.olMailItem Then Exit Sub
to
If Item.Class 43 Then Exit Sub
Otherwise I never get an information.
Or am I doing something wrong?
Regards,
Michael
something is not right as the message class did not change between versions.
Is it possible to monitor multiple secondary mailboxes?
You need to repeat this line with a new variable (and the associated lines)
Set objNewFolderItems = GetFolderPath("Secondary Mailbox Name\Inbox").Items
Duplicate the objNewMailItems_ItemAdd macro.
Hi Diane, thanks for your articles, they are very useful. In the meantime I do not get why do we declare "Dim objMyInbox As Outlook.MAPIFolder" , and do nothing with this object except setting it to Nothing before End Sub.
Thanks.
Because I forgot to remove it when i edited a different macro. :(