Reputation: 2571
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.
Upvotes: 1
Views: 4031
Reputation: 1
In my case, I need to trigger an event after a new email is received & after email sync so that I get a new email, or else I will not receive a new email attachment.
Below my solution may help you.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += Application_NewMail;
}
private void Application_NewMail()
{
_currentExplorer = Application.ActiveExplorer();
_currentExplorer.Session.SyncObjects[1].SyncEnd += AppFolders_SyncEnd;
_currentExplorer.Session.SyncObjects[1].Start();
}
private void AppFolders_SyncEnd()
{
//Your enter code here
}
Upvotes: 0
Reputation: 755
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
}
Upvotes: 0
Reputation: 2571
I tried
Application.ActiveExplorer().SyncObjects.AppFolders.SyncEnd += \\EventHandler
This hooks on to send/receive of all default folders..
Upvotes: 2
Reputation: 2720
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);
}
}
Upvotes: 0