Question

How can I attach an event handler for SendAndReceive event of Contact folders/Contact Items in Outlook 2007 using VSTO AddIn? I tried using:

Application.ActiveExplorer().SyncObjects.ForEach
{
   SyncObject.SyncEnd += \\Do something
}

But it is not working.

Was it helpful?

Solution

I tried

Application.ActiveExplorer().SyncObjects.AppFolders.SyncEnd += \\EventHandler

This hooks on to send/receive of all default folders..

OTHER TIPS

Actually my need was a bit different but may be the same: I wanted to be notified of the changes of a folder (appointments in my case) after a send/receive. My first thought (and I think you are on the same track) was to check for a send/receive event and maybe get some collection of items out of it or something similar, but no such thing is available. (as is also explained in this forum post)

My second path came from the following article: I can register to the Item_Add and Item_Change (and even Item_Removed) event of a folder (whom are also triggered by the changes done by a send receive):

Some code:

// Get the folder calendar folder and subscribe to the events.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
    Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items.ItemChange += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);
}

// Do something with it.
void Items_ItemAdd(object Item)
{
    logItem(Item, "Add");
}
void logItem(object Item, string Action)
{

    Outlook.AppointmentItem item = Item as Outlook.AppointmentItem;

    File.AppendAllText(@"e:\log.txt", string.Format("Item {0}: {1}", Action, Item));

    if (item != null)
    {
        File.AppendAllText(@"e:\log.txt", " - Appointment: " + item.Subject);
    }
}

You can hook up the mail send/receive event and then check that the mail type is a ContactItem. Here is an example of the Send event.

// hook up the event
this.Application.ItemSend += ThisApplication_SentMail;

then in your event handler you check the mail item type;

internal void ThisApplication_SentMail(object item, ref bool cancel)
{
    Outlook.ContactItem contactItem = item as Outlook.ContactItem;

    // mail message is not a ContactItem, so exit.
    if (contactItem == null) return;

    // do  whatever you need to here

 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top