Log in

View Full Version : NewMail event



walduxas
01-31-2012, 12:50 AM
Hi,

I have a little problem when I need to trigger NewMail event in Outlook VBA. I wrote this code in ThisOutlookSession:

Private Sub Application_NewMail()

Call CheckforNewDocConMail

End Sub

The problem is that in my outlook application there are two mailboxes (see attachment):
1) Mailbox - My name;
2) Mailbox - company name.

The first mailbox is my personal mailbox and the second mailbox is shared mailbox and may colleagues also have connection to this mailbox. And it seems that NewMail event is triggered only when I receive new mail into my personal mailbox, however, I need this code to be triggered when the new mail comes into the shared mailbox.

Is there any way to do that?

Kind regards,
walduxas

JP2112
02-02-2012, 08:34 AM
Yes -- in Application_Startup, set object references to both mailboxes. These need to be event-enabled using the WithEvents keyword:

Private WithEvents MyNameItems As Outlook.Items
Private WithEvents MyCompanyNameItems As Outlook.Items

Then use the ItemAdd event to monitor when new items come into each mailbox's Inbox folder and run your custom code. For example code see Stock Event Code (http://www.jpsoftwaretech.com/outlook-vba/stock-event-code/).