Home > other >  VSTO Outlook ItemAdd not being fired when incomming e-mail is moved by rule to subfolder
VSTO Outlook ItemAdd not being fired when incomming e-mail is moved by rule to subfolder

Time:12-01

I have an Outlook VSTO add-in. I want to respond to incoming emails. This works quite well with the declaration

Public WithEvents items As Outlook.Items 

And the definition for the items that are observed. (I'm afraid that's why only "Inbox" is watched):

inbox = objOutlook.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
items = inbox.Items

and the eventhandler

Private Sub Items_ItemAdd(ByVal item As Object) Handles items.ItemAdd 

Even if Outlook is closed, an event is triggered for each new email when Outlook is started.

I've now noticed that some users of the add-in have created a rule that moves incoming emails to a subfolder of "Inbox". In this case, the Items_ItemAdd event is not fired when a new email arrives.

How can I also capture these new emails that are moved via a rule?

CodePudding user response:

Use the NewMailEx event of the Application class which is fired 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.

The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. Use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.

Also you may track the items in the folders by combining multiple mechanisms like this event handler plus getting new items at startup to not skip any of them that was added silently. The Find/FindNext or Restrict methods of the Items class can help you with such tasks. Read more about that in the series of articles:

CodePudding user response:

The item is moved on the server side by the server side rule, and even if Outlook was running, it could've been moved before Outlook downloaded the original item to the OST store. So if ItemAdd event fires, it only fires on the folder where the item was moved to, not in the Inbox folder.

Besides watching the Inbox folder, you'd need to watch the other folders that the rules might point to - retrieve the rules using Store.GetRules(), loop through all rules, check if Rule.MoveToFolder action is enabled, and if yes, retrieve the target folder from MoveOrCopyRuleAction.Folder property.

  • Related