views:

626

answers:

3

How to 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 }

It is not working

A: 

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);
    }
}
Cohen
A: 

You can hook up the mail send/revice event and then check the mail type is a contactItem here is an exaple of the send event.

hook up the event

this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(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 = null;

    contactItem = item as Outlook.ContactItem;

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

    //do  what ever you need to here

 }
squig
A: 

I tried

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

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

Deepak N