watching folder for changes

Jun 24, 2016 at 6:23 PM
Hi,

I wanted to ask how to register for a change of items in a folder. It seems like I can't get access to the event.

In VSTO I would write for example:
var folder = this.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderToDo);
((ItemsEvents_Event)folder.Items).ItemAdd += item => { ... }
It looks like ItemsEvents_Event does not exist in Netoffice?

thanks,
Martin
Coordinator
Jun 24, 2016 at 11:04 PM
Of course NetOffice want support ItemsEvents_Event.
Any event name in NetOffice comes with "Event" as postfix.
So "ItemAdd" is now "ItemAddEvent" in NetOffice.

The reason is because a lot of events in office classes use events/properties/methods with the same name.
For example Excel.Range has an "Activate();" method and an "event Activate;" event. (Moreover Excel.Range has a method called Range but the name of the class in .Net is preserved for the .ctor) This is allowed in COM (which Office comes from) but not suitable in .Net Languages.

In order to prevent this downcast(you do here) NetOffice do this one breaking syntax change for events.
(Other COM/.Net issues with Properties/methods with same name was resolved by an intermediate base class..so the base class can have the same property name like the top class methods...only visual basic.net face some problems here...)

let me know this helps
Sebastian
Jun 26, 2016 at 8:04 PM
Hi Sebastian,

thanks for yor reply. As a result I have finally been able to find the reason for my problem:
I need to cast the Items collection to Outlook.Items.

I wanted to suggest to add this use-case to the tutorials or examples.

For me the following code works now:
        var todoFolder = session.GetDefaultFolder(OlDefaultFolders.olFolderToDo);
        var todoFolderItems = (Outlook.Items)todoFolder.Items;
        todoFolderItems.ItemAddEvent += Items_ItemAddEvent;
        todoFolderItems.ItemRemoveEvent += Items_ItemRemoveEvent;
        todoFolderItems.ItemChangeEvent += Items_ItemChangeEvent;
thanks for your help,
Martin