Willy
Willy

Reputation: 10638

VSTO Outlook: Items.ItemAdd on Inbox, sent and draft folders -> Detect if it is fired as a result of a movement action caused by user

I am trying to detect when an item is moved from one folder to another because in this use case I need to perform some actions. I need to detect when event is fired by a movement action caused by the user when he moves explicitly an item from one folder to another.

The problem is when Items.ItemAdd is fired for inbox, draft and sent folders because here I have a problem for these folders, I mean:

Same applies for other both folders, draft and sent:

also it would be applicable for other folders such as junk folder, how to detect if its a junk email received and put there by Outlook itself (not by user) or if the event is fired just because the user explicitly moved it to there?

What I am thinking about is just when the item is being moved, in the Folder_BeforeItemMove, set a property for the Outlook.MailItem, let's say for example, moved, so in the Items.ItemAdd I would check if that property exists for the Outlook.MailItem, if it exists then it is a movement caused explicitly by the user, otherwise, if this property does not exist, then the movement action to that folder has been caused by Outlook itself. Is that a good idea? of course, once the item is processed in the Items.ItemAdd event, i need to remove this property from the Outlook.MailItem. y

Upvotes: 0

Views: 140

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Not 100% foolproof, but try to check if MailItem.LastModificationTime is close enough to MailItem.CreationTime. Don't compare the values to the local time since local and server clocks can be off.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

The Outlook object model doesn't recognize actions made manually and programmatically. Only tracking the sequence of actions can help you to detect the operation - whether a new item is received from the server (see the NewMailEx event of the Application class), autosaved (new items don't have EntryID property set until they are saved to the store and etc. The BeforeItemMove event can also help in distinguishing events made manually or not.

You are free to add any extra bits to items to distinguish actions, but I'd recommend recording an item's data instead, so the item will be unmodified. And when the ItemAdd event is fired you may compare the data recorded (from a collection for example) and find whether the BeforeItemMove event was raised previously. Be aware, actions can be cancelled, so you may get one event and never the other. So, you need to flush that data at some point.

Upvotes: 1

Related Questions