0
votes

I'm using Outlook 2016 with two e-mail accounts and I've set up a rule to call a VBA script to do some stuff with the incoming messages on one of the accounts. Is there a way, using VBA, to access the email object that triggered the rule I'm using?

What I need is just to get the sender's e-mail of each message.

PS: I'm using POP3, not Exchange. Also, I've tried Application_NewMail() instead of a rule but it didn't work.

1

1 Answers

0
votes

The VBA macro sub should look like the following one when you assign it to a rule:

Public Sub Test(mail as MailItem)
   ' where mail is the object which triggered the rule
   MsgBox mail.SenderEmailAddress
End Sub

So, you can get the sender's email address out of the box.

Also as a possible workaround to Outlook rules you may consider handling the NewMailEx event of the Application class which is fired when a new item is received in the Inbox.

This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem , MeetingItem , or SharingItem . The EntryIDsCollection string contains the Entry ID that corresponds to that item. Note that this behavior has changed from earlier versions of the event when the EntryIDCollection contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired.

The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.