Reputation: 176
I am writing an Outlook Add-in and want to do something (not relevant here) with the data of an Appointment after (when) it was saved.
(i am new to Outlook-Addins)
so i found that there is an AfterWrite event where i can register a Method.
And there is an ItemLoad event on Application.
so my first Efford was something like this:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// ...
this.Application.ItemLoad +=
new Outlook.ApplicationEvents_11_ItemLoadEventHandler(atItemLoad);
}
public void atItemLoad(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
aitem.AfterWrite +=
new Outlook.ItemEvents_10_AfterWriteEventHandler(afterWrite);
}
}
public void afterWrite()
{
// Who was written?
MessageBox.Show("it was written!");
}
The problem is, that I don't know how to get to the data of the Appointment, that fired the event.
Application.ItemLoad registers a function that gets an Object, that can be cast to an Appointment.
AfterWrite does not. I'd like something like this:
public void afterWrite(Outlook.AppointmentItem aitem)
{
// do something with the data from the Appointment
MessageBox.Show(aitem.Subject + " was written!");
}
I fear that I am researching in the totally wrong direction..
*sorry if my english is a mess - it's not my mother language
edit:
i even tried a construct like this:
private List<AppointmentEventHolder> holderList = new List<AppointmentEventHolder>();
internal class AppointmentEventHolder
{
private Outlook.AppointmentItem aitem = null;
public AppointmentEventHolder(Outlook.AppointmentItem item)
{
aitem = item;
}
public void onWrite()
{
MessageBox.Show("write: " + aitem.Subject);
}
}
public void atItemLoad(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
AppointmentEventHolder aHolder = new AppointmentEventHolder(aitem);
holderList.Add(aHolder);
aitem.AfterWrite += aHolder.onWrite;
}
}
but event doesn't get fired! i am very frustrated now
Upvotes: 5
Views: 6525
Reputation: 176
ok, i think i got this.
You have to register a handler on the Folder, that has your Appointments.
i read and experienced myself, that its crutial to save a reference to the objects you register an event, so i did this.
So thats the code
private Outlook.MAPIFolder _CalendarFolder = null;
private Outlook.Items _CalendarItems = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.MAPIFolder calendarFolder =
this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
// get the Termine24-Folder (if not found, create it)
try
{
_CalendarFolder = calendarFolder.Folders[Constants.FOLDERNAME];
}
catch
{
_CalendarFolder = calendarFolder.Folders.Add(Constants.FOLDERNAME);
}
_CalendarItems = _CalendarFolder.Items;
attachEvents();
}
public void attachEvents()
{
_CalendarItems.ItemAdd += Item_Add;
_CalendarItems.ItemChange += Item_Change;
_CalendarItems.ItemRemove += Item_Remove;
}
public void Item_Add(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
MessageBox.Show("add: " + aitem.Subject);
}
else
{
MessageBox.Show("add, but no appointment");
}
}
public void Item_Change(Object item)
{
Outlook.AppointmentItem aitem = item as Outlook.AppointmentItem;
if (aitem != null)
{
MessageBox.Show("changed: " + aitem.Subject);
}
else
{
MessageBox.Show("change, but no appointment");
}
}
public void Item_Remove()
{
MessageBox.Show("Item_remove");
}
to good thing is, that now I also get an event if the appointment gets drag-and-dropped in the overview.
BUT the Item_Remove still don't gets me the Object that got removed.
Upvotes: 6