Outlook Banter

Outlook Banter (http://www.outlookbanter.com/)
-   Add-ins for Outlook (http://www.outlookbanter.com/add-ins-outlook/)
-   -   Why does Outlook 2007 generate 3 events for 1 single "Add"? (http://www.outlookbanter.com/add-ins-outlook/88995-why-does-outlook-2007-generate.html)

pkelley April 13th 09 06:56 PM

Why does Outlook 2007 generate 3 events for 1 single "Add"?
 
Using Windows XP, running Outlook 2007, C#, Visual Studio 2005 with VSTO SE.
I have an Outlook AddIn that monitors calendar Appointments.
The AddIn is working great, but I've noticed a strange anomoly:

If I add a NEW Appointment, I expect to that my "Calendar_ItemAdd" event
handler will fire (only once).
If I change an EXISTING Appointment, I expect that my
"Calendar_ItemChange" event handler will fire (only once).

Real Behavior:
If I add a NEW Appointment, my "Calendar_ItemAdd" event handler fires (as
expected), but then my "Calendar_ItemChange" event fires 15 seconds later,
and then my "Calendar_ItemChange" event fires AGAIN, another 15 seconds later.

I'm confused. Why is Outlook 2007 calling my "Calendar_ItemChange" event
handler, when I create a single, NEW Appointment?

Here's the code that monitors the "OlDefaultFolders.olFolderCalendar" folder:

public class CalendarMonitor
{
private NameSpace m_session;
private Liststring m_folderPaths;
private ListMAPIFolder m_calendarFolders;
private ListItems m_calendarItems;
private ListItems m_deletedItems;
private MAPIFolder m_deletedItemsFolder;
private MAPIFolder m_calFolder;
public event EventHandlerEventArgsAppointmentItem
AppointmentAdded;
public event EventHandlerEventArgsAppointmentItem
AppointmentModified;
public event EventHandlerCancelEventArgsAppointmentItem
AppointmentDeleting;

// Constructor
public CalendarMonitor(NameSpace session)
{
m_folderPaths = new Liststring();
m_calendarFolders = new ListMAPIFolder();
m_calendarItems = new ListItems();
m_deletedItems = new ListItems();
m_session = session;
m_deletedItemsFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder DeletedItems);
m_calFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder Calendar);
HookupDefaultCalendarEvents();
} // End constructor CalendarMonitor()

public void Shutdown()
{
UnhookCalendarEvents();
m_folderPaths.Clear();
m_folderPaths = null;
m_calendarFolders.Clear();
m_calendarFolders = null;
m_calendarItems.Clear();
m_calendarItems = null;
m_deletedItems.Clear();
m_deletedItems = null;
m_session = null;
m_deletedItemsFolder = null;
m_calFolder = null;
AppointmentAdded = null;
AppointmentModified = null;
AppointmentDeleting = null;
} // End Shutdown()

private void HookupDefaultCalendarEvents()
{
if ((m_calFolder != null) && (m_deletedItemsFolder != null))
{
HookupCalendarEvents();
}
} // End HookupDefaultCalendarEvents()

private void HookupCalendarEvents()
{
if (m_calFolder.DefaultItemType != OlItemType.olAppointmentItem)
{
throw new ArgumentException("The MAPIFolder 'm_calFolder'
must use AppointmentItems as the default type.");
}

// Check for duplicate entries. Helps prevent double-ups on
event listeners.
if (m_folderPaths.Contains(m_calFolder.FolderPath) == false)
{
Items items = m_calFolder.Items;
m_folderPaths.Add(m_calFolder.FolderPath);

Items delItems = m_deletedItemsFolder.Items;

// Storing a reference to the folder and to the items
collection
// keeps folder alive. This keeps the ref count up and
prevents
// the problem of intermittent release of the COM object due
to
// garbage collection, which in turn causes events to NOT
fire.
m_calendarFolders.Add(m_calFolder);
m_calendarItems.Add(items);
m_deletedItems.Add(delItems);

// Now add event listeners.
items.ItemChange +=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
items.ItemAdd +=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
delItems.ItemAdd +=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void UnhookCalendarEvents()
{
foreach (Items curItem in m_calendarItems)
{
curItem.ItemChange -=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
curItem.ItemAdd -=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
}
foreach (Items curItem in m_deletedItems)
{
curItem.ItemAdd -=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void Calendar_ItemAdd(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentAdded != null)
{
// Notify "Add" event listener.
this.AppointmentAdded(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemChange(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentModified != null)
{
// Notify "Modify" event listener.
this.AppointmentModified(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemDelete(object item)
{
if (item is AppointmentItem)
{
if (this.AppointmentDeleting != null)
{
CancelEventArgsAppointmentItem args =
new
CancelEventArgsAppointmentItem((AppointmentItem) item);
this.AppointmentDeleting(this, args);
}
}
}
} // End class CalendarMonitor

public class EventArgsT : EventArgs
{
private T m_value;

public EventArgs(T aValue)
{
m_value = aValue;
}

public T Value
{
get { return m_value; }
set { m_value = value; }
}
}


Ken Slovak - [MVP - Outlook] April 14th 09 02:42 PM

Why does Outlook 2007 generate 3 events for 1 single "Add"?
 
Your Delete signature is incorrect, no item is passed to tell you what was
deleted.

What exactly is being done when ItemAdd fires, are you doing anything with
that item and its properties?

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"pkelley" wrote in message
...
Using Windows XP, running Outlook 2007, C#, Visual Studio 2005 with VSTO
SE.
I have an Outlook AddIn that monitors calendar Appointments.
The AddIn is working great, but I've noticed a strange anomoly:

If I add a NEW Appointment, I expect to that my "Calendar_ItemAdd" event
handler will fire (only once).
If I change an EXISTING Appointment, I expect that my
"Calendar_ItemChange" event handler will fire (only once).

Real Behavior:
If I add a NEW Appointment, my "Calendar_ItemAdd" event handler fires
(as
expected), but then my "Calendar_ItemChange" event fires 15 seconds later,
and then my "Calendar_ItemChange" event fires AGAIN, another 15 seconds
later.

I'm confused. Why is Outlook 2007 calling my "Calendar_ItemChange" event
handler, when I create a single, NEW Appointment?

Here's the code that monitors the "OlDefaultFolders.olFolderCalendar"
folder:

public class CalendarMonitor
{
private NameSpace m_session;
private Liststring m_folderPaths;
private ListMAPIFolder m_calendarFolders;
private ListItems m_calendarItems;
private ListItems m_deletedItems;
private MAPIFolder m_deletedItemsFolder;
private MAPIFolder m_calFolder;
public event EventHandlerEventArgsAppointmentItem
AppointmentAdded;
public event EventHandlerEventArgsAppointmentItem
AppointmentModified;
public event EventHandlerCancelEventArgsAppointmentItem
AppointmentDeleting;

// Constructor
public CalendarMonitor(NameSpace session)
{
m_folderPaths = new Liststring();
m_calendarFolders = new ListMAPIFolder();
m_calendarItems = new ListItems();
m_deletedItems = new ListItems();
m_session = session;
m_deletedItemsFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder DeletedItems);
m_calFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder Calendar);
HookupDefaultCalendarEvents();
} // End constructor CalendarMonitor()

public void Shutdown()
{
UnhookCalendarEvents();
m_folderPaths.Clear();
m_folderPaths = null;
m_calendarFolders.Clear();
m_calendarFolders = null;
m_calendarItems.Clear();
m_calendarItems = null;
m_deletedItems.Clear();
m_deletedItems = null;
m_session = null;
m_deletedItemsFolder = null;
m_calFolder = null;
AppointmentAdded = null;
AppointmentModified = null;
AppointmentDeleting = null;
} // End Shutdown()

private void HookupDefaultCalendarEvents()
{
if ((m_calFolder != null) && (m_deletedItemsFolder != null))
{
HookupCalendarEvents();
}
} // End HookupDefaultCalendarEvents()

private void HookupCalendarEvents()
{
if (m_calFolder.DefaultItemType !=
OlItemType.olAppointmentItem)
{
throw new ArgumentException("The MAPIFolder 'm_calFolder'
must use AppointmentItems as the default type.");
}

// Check for duplicate entries. Helps prevent double-ups on
event listeners.
if (m_folderPaths.Contains(m_calFolder.FolderPath) == false)
{
Items items = m_calFolder.Items;
m_folderPaths.Add(m_calFolder.FolderPath);

Items delItems = m_deletedItemsFolder.Items;

// Storing a reference to the folder and to the items
collection
// keeps folder alive. This keeps the ref count up and
prevents
// the problem of intermittent release of the COM object
due
to
// garbage collection, which in turn causes events to NOT
fire.
m_calendarFolders.Add(m_calFolder);
m_calendarItems.Add(items);
m_deletedItems.Add(delItems);

// Now add event listeners.
items.ItemChange +=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
items.ItemAdd +=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
delItems.ItemAdd +=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void UnhookCalendarEvents()
{
foreach (Items curItem in m_calendarItems)
{
curItem.ItemChange -=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
curItem.ItemAdd -=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
}
foreach (Items curItem in m_deletedItems)
{
curItem.ItemAdd -=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void Calendar_ItemAdd(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentAdded != null)
{
// Notify "Add" event listener.
this.AppointmentAdded(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemChange(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentModified != null)
{
// Notify "Modify" event listener.
this.AppointmentModified(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemDelete(object item)
{
if (item is AppointmentItem)
{
if (this.AppointmentDeleting != null)
{
CancelEventArgsAppointmentItem args =
new
CancelEventArgsAppointmentItem((AppointmentItem) item);
this.AppointmentDeleting(this, args);
}
}
}
} // End class CalendarMonitor

public class EventArgsT : EventArgs
{
private T m_value;

public EventArgs(T aValue)
{
m_value = aValue;
}

public T Value
{
get { return m_value; }
set { m_value = value; }
}
}



Dmitry Streblechenko April 14th 09 04:07 PM

Why does Outlook 2007 generate 3 events for 1 single "Add"?
 
Why are you suprised? Something modifies an item after it is created...
--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"pkelley" wrote in message
...
Using Windows XP, running Outlook 2007, C#, Visual Studio 2005 with VSTO
SE.
I have an Outlook AddIn that monitors calendar Appointments.
The AddIn is working great, but I've noticed a strange anomoly:

If I add a NEW Appointment, I expect to that my "Calendar_ItemAdd" event
handler will fire (only once).
If I change an EXISTING Appointment, I expect that my
"Calendar_ItemChange" event handler will fire (only once).

Real Behavior:
If I add a NEW Appointment, my "Calendar_ItemAdd" event handler fires
(as
expected), but then my "Calendar_ItemChange" event fires 15 seconds later,
and then my "Calendar_ItemChange" event fires AGAIN, another 15 seconds
later.

I'm confused. Why is Outlook 2007 calling my "Calendar_ItemChange" event
handler, when I create a single, NEW Appointment?

Here's the code that monitors the "OlDefaultFolders.olFolderCalendar"
folder:

public class CalendarMonitor
{
private NameSpace m_session;
private Liststring m_folderPaths;
private ListMAPIFolder m_calendarFolders;
private ListItems m_calendarItems;
private ListItems m_deletedItems;
private MAPIFolder m_deletedItemsFolder;
private MAPIFolder m_calFolder;
public event EventHandlerEventArgsAppointmentItem
AppointmentAdded;
public event EventHandlerEventArgsAppointmentItem
AppointmentModified;
public event EventHandlerCancelEventArgsAppointmentItem
AppointmentDeleting;

// Constructor
public CalendarMonitor(NameSpace session)
{
m_folderPaths = new Liststring();
m_calendarFolders = new ListMAPIFolder();
m_calendarItems = new ListItems();
m_deletedItems = new ListItems();
m_session = session;
m_deletedItemsFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder DeletedItems);
m_calFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder Calendar);
HookupDefaultCalendarEvents();
} // End constructor CalendarMonitor()

public void Shutdown()
{
UnhookCalendarEvents();
m_folderPaths.Clear();
m_folderPaths = null;
m_calendarFolders.Clear();
m_calendarFolders = null;
m_calendarItems.Clear();
m_calendarItems = null;
m_deletedItems.Clear();
m_deletedItems = null;
m_session = null;
m_deletedItemsFolder = null;
m_calFolder = null;
AppointmentAdded = null;
AppointmentModified = null;
AppointmentDeleting = null;
} // End Shutdown()

private void HookupDefaultCalendarEvents()
{
if ((m_calFolder != null) && (m_deletedItemsFolder != null))
{
HookupCalendarEvents();
}
} // End HookupDefaultCalendarEvents()

private void HookupCalendarEvents()
{
if (m_calFolder.DefaultItemType !=
OlItemType.olAppointmentItem)
{
throw new ArgumentException("The MAPIFolder 'm_calFolder'
must use AppointmentItems as the default type.");
}

// Check for duplicate entries. Helps prevent double-ups on
event listeners.
if (m_folderPaths.Contains(m_calFolder.FolderPath) == false)
{
Items items = m_calFolder.Items;
m_folderPaths.Add(m_calFolder.FolderPath);

Items delItems = m_deletedItemsFolder.Items;

// Storing a reference to the folder and to the items
collection
// keeps folder alive. This keeps the ref count up and
prevents
// the problem of intermittent release of the COM object
due
to
// garbage collection, which in turn causes events to NOT
fire.
m_calendarFolders.Add(m_calFolder);
m_calendarItems.Add(items);
m_deletedItems.Add(delItems);

// Now add event listeners.
items.ItemChange +=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
items.ItemAdd +=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
delItems.ItemAdd +=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void UnhookCalendarEvents()
{
foreach (Items curItem in m_calendarItems)
{
curItem.ItemChange -=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
curItem.ItemAdd -=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
}
foreach (Items curItem in m_deletedItems)
{
curItem.ItemAdd -=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void Calendar_ItemAdd(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentAdded != null)
{
// Notify "Add" event listener.
this.AppointmentAdded(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemChange(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentModified != null)
{
// Notify "Modify" event listener.
this.AppointmentModified(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemDelete(object item)
{
if (item is AppointmentItem)
{
if (this.AppointmentDeleting != null)
{
CancelEventArgsAppointmentItem args =
new
CancelEventArgsAppointmentItem((AppointmentItem) item);
this.AppointmentDeleting(this, args);
}
}
}
} // End class CalendarMonitor

public class EventArgsT : EventArgs
{
private T m_value;

public EventArgs(T aValue)
{
m_value = aValue;
}

public T Value
{
get { return m_value; }
set { m_value = value; }
}
}




pkelley April 14th 09 04:56 PM

Why does Outlook 2007 generate 3 events for 1 single "Add"?
 
With regard to Delete:

In the constructor method "CalendarMonitor" I get a handle to the deleted
items folder.
Then in "HookupCalendarEvents()" I'm trying to handle the ItemAdd event of
the deleted
items folder so whenever an item is moved to the deleted items folder the
"Calendar_ItemDelete" event
handler fires and "Calendar_ItemDelete" is passed the item added to the
delete folder.

Also, in the constructor "CalendarMonitor" I get a handle to the calendar
folder.
Then in "HookupCalendarEvents()" I handle the ItemAdd and ItemChange events
of the calendar
folder.

In all three cases, Calendar_ItemChange, Calendar_ItemAdd,
Calendar_ItemDelete seem to receive
the proper Item for processing. Do you still see something I'm missing? If
yes, what would you
do to handle the ItemAdd event to the deleted items folder?


With regard to the ItemAdd Event, the Event Handlers are in my "ThisAddIn"
class:

public partial class ThisAddIn
{
#region Instance Variables

private Outlook.Inspectors m_Inspectors; // Outlook
inspectors collection
private Outlook.NameSpace m_nameSpace;
internal static ListOutlookInspector m_Windows; // List of tracked
inspector windows
internal static Office.IRibbonUI m_Ribbon; // Ribbon UI
reference
private CalendarMonitor m_monitor; // Calendar
appointment events.
private Outlook.AppointmentItem m_olAppointment;

#endregion

#region VSTO Startup and Shutdown methods

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Initialize variables
m_Inspectors = this.Application.Inspectors;
m_nameSpace = this.Application.GetNamespace("MAPI");
m_Windows = new ListOutlookInspector();
m_monitor = new CalendarMonitor(this.Application.Session);

// Wire up event handlers
m_monitor.AppointmentAdded +=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentAdded);
m_monitor.AppointmentDeleting +=
new
EventHandlerCancelEventArgsMicrosoft.Office.Inte rop.Outlook.AppointmentItem(Monitor_AppointmentD eleting);
m_monitor.AppointmentModified +=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentModifie d);
m_Inspectors.NewInspector +=
new
Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Unhook event handlers
if (m_odbcForm != null)
{
m_odbcForm.ODBCConnectionStatus -=
new
EventHandlerEventArgsbool(SetODBCConnectionSta tus);
}
m_monitor.AppointmentAdded -=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentAdded);
m_monitor.AppointmentDeleting -=
new
EventHandlerCancelEventArgsMicrosoft.Office.Inte rop.Outlook.AppointmentItem(Monitor_AppointmentD eleting);
m_monitor.AppointmentModified -=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentModifie d);
m_monitor.Shutdown();
m_odbcForm.FormShutdown();
m_Inspectors.NewInspector -= new
Microsoft.Office.Interop.Outlook.InspectorsEvents_ NewInspectorEventHandler(Inspectors_NewInspector);

// Dereference objects
m_Inspectors = null;
m_Windows.Clear();
m_Windows = null;
m_Ribbon = null;
m_nameSpace = null;
m_monitor = null;
m_olAppointment = null;
}

#endregion

#region Methods

/// summary
/// Looks up the window wrapper for a given window object
/// /summary
/// param name="window"An outlook inspector window/param
/// returns/returns
internal static OutlookInspector FindOutlookInspector(object window)
{
foreach (OutlookInspector inspector in m_Windows)
{
if (inspector.Window == window)
{
return inspector;
}
}
return null;
}

/// summary
/// Iterates through a list of Appointment "Outlook.Recipients" and
/// has Microsoft Exchange identify their SMTP email addresses.
/// This routine then concatenates the email address to the end of a
/// string and separates each email address using a semi-colon ";"
/// followed by a space " " character.
/// The returned string is trimmed of spaces (and tabs) leaving a
/// string containing email addresses separated and terminated by
/// a semi-colon ";"
/// /summary
/// param name="recipients"A collection of Recipient
objects./param
/// returnsA string object containing Appointment Attendee email
addresses.
/// For Example:
/// ; ;
;"
/// /returns
private string
GetAttendees(Microsoft.Office.Interop.Outlook.Reci pients recipients)
{
string attendees = null;
Microsoft.Office.Interop.Outlook.ExchangeUser exUser = null;
string smtpAddr = null;
foreach (Microsoft.Office.Interop.Outlook.Recipient recipient in
recipients)
{
exUser = recipient.AddressEntry.GetExchangeUser();
if (exUser == null)
{
smtpAddr = recipient.Address;
}
else
{
smtpAddr = exUser.PrimarySmtpAddress;
}
attendees += (smtpAddr + "; ");
}
if (attendees != null)
return attendees.Trim();
else
return ;";
}

private string GetFromEMailAddress(string fromName)
{
string emailAddr = ";
Outlook.Recipient recip = m_nameSpace.CreateRecipient(fromName);
Outlook.ExchangeUser exchgUser =
recip.AddressEntry.GetExchangeUser();
if (exchgUser != null)
{
emailAddr = exchgUser.PrimarySmtpAddress;
}
return emailAddr;
}

#endregion

#region Event Handlers

private void SetODBCConnectionStatus(object sender, EventArgsbool
connected)
{
m_odbcIsActive = connected.Value;
if (m_ribbon != null)
{
m_ribbon.SetConnectedToDB(m_odbcIsActive);
}
}

private void Monitor_AppointmentAdded(object sender,
EventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "NEW";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. ADD to Cache ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}
}

private void Monitor_AppointmentDeleting(object sender,
CancelEventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "DELETE";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. DELETE from Cache ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}

// We could prevent the delete from happening if we wanted to,
// but we don't so allow the delete to occur.
appt.Cancel = false;
}

/// summary
/// This method is called immediately following the "Add" event of
/// a Calendar AppointmentItem.
/// This method is also called immediately following the
"Modification"
/// event of a Calendar AppointmentItem.
/// /summary
/// param name="sender"The sending object/param
/// param name="appt"The calendar appointment record
modified/param
private void Monitor_AppointmentModified(object sender,
EventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "CHANGE";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. CHANGE Cache value ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}
}

/// summary
/// The NewInspector event fires whenever a new inspector is
displayed. We use
/// this event to add our custom button to appointment item
inspectors.
/// /summary
/// param name="Inspector"/param
private void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
try
{
// Debug.WriteLine("Inspectors_NewInspector");
OutlookItem olItem = new OutlookItem(Inspector.CurrentItem);

// Make sure this is an appointment item
if (olItem.Class == Outlook.OlObjectClass.olAppointment)
{
m_olAppointment =
(Outlook.AppointmentItem)Inspector.CurrentItem;
m_ribbon.setApptItem(m_olAppointment);

// Check to see if this is a new window
// we don't already track
OutlookInspector existingWindow =
FindOutlookInspector(Inspector);
// If the m_Windows collection does not
// have a window for this Inspector,
// we should add it to m_Windows
if (existingWindow == null)
{
OutlookInspector window = new
OutlookInspector(Inspector);
window.Close += new EventHandler(WrappedWindow_Close);
window.InvalidateControl +=
new
EventHandlerEventArgsstring(WrappedWindow_Inva lidateControl);
m_Windows.Add(window);
}

}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}

void WrappedWindow_InvalidateControl(object sender,
EventArgsstring e)
{
if (m_Ribbon != null)
{
m_Ribbon.InvalidateControl(e.Value);
}
}

void WrappedWindow_Close(object sender, EventArgs e)
{
OutlookInspector window = (OutlookInspector)sender;
window.Close -= new EventHandler(WrappedWindow_Close);
m_Windows.Remove(window);
}

#endregion

#region VSTO generated code

/// summary
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// /summary
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}


"Ken Slovak - [MVP - Outlook]" wrote:

Your Delete signature is incorrect, no item is passed to tell you what was
deleted.

What exactly is being done when ItemAdd fires, are you doing anything with
that item and its properties?

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"pkelley" wrote in message
...
Using Windows XP, running Outlook 2007, C#, Visual Studio 2005 with VSTO
SE.
I have an Outlook AddIn that monitors calendar Appointments.
The AddIn is working great, but I've noticed a strange anomoly:

If I add a NEW Appointment, I expect to that my "Calendar_ItemAdd" event
handler will fire (only once).
If I change an EXISTING Appointment, I expect that my
"Calendar_ItemChange" event handler will fire (only once).

Real Behavior:
If I add a NEW Appointment, my "Calendar_ItemAdd" event handler fires
(as
expected), but then my "Calendar_ItemChange" event fires 15 seconds later,
and then my "Calendar_ItemChange" event fires AGAIN, another 15 seconds
later.

I'm confused. Why is Outlook 2007 calling my "Calendar_ItemChange" event
handler, when I create a single, NEW Appointment?

Here's the code that monitors the "OlDefaultFolders.olFolderCalendar"
folder:

public class CalendarMonitor
{
private NameSpace m_session;
private Liststring m_folderPaths;
private ListMAPIFolder m_calendarFolders;
private ListItems m_calendarItems;
private ListItems m_deletedItems;
private MAPIFolder m_deletedItemsFolder;
private MAPIFolder m_calFolder;
public event EventHandlerEventArgsAppointmentItem
AppointmentAdded;
public event EventHandlerEventArgsAppointmentItem
AppointmentModified;
public event EventHandlerCancelEventArgsAppointmentItem
AppointmentDeleting;

// Constructor
public CalendarMonitor(NameSpace session)
{
m_folderPaths = new Liststring();
m_calendarFolders = new ListMAPIFolder();
m_calendarItems = new ListItems();
m_deletedItems = new ListItems();
m_session = session;
m_deletedItemsFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder DeletedItems);
m_calFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder Calendar);
HookupDefaultCalendarEvents();
} // End constructor CalendarMonitor()

public void Shutdown()
{
UnhookCalendarEvents();
m_folderPaths.Clear();
m_folderPaths = null;
m_calendarFolders.Clear();
m_calendarFolders = null;
m_calendarItems.Clear();
m_calendarItems = null;
m_deletedItems.Clear();
m_deletedItems = null;
m_session = null;
m_deletedItemsFolder = null;
m_calFolder = null;
AppointmentAdded = null;
AppointmentModified = null;
AppointmentDeleting = null;
} // End Shutdown()

private void HookupDefaultCalendarEvents()
{
if ((m_calFolder != null) && (m_deletedItemsFolder != null))
{
HookupCalendarEvents();
}
} // End HookupDefaultCalendarEvents()

private void HookupCalendarEvents()
{
if (m_calFolder.DefaultItemType !=
OlItemType.olAppointmentItem)
{
throw new ArgumentException("The MAPIFolder 'm_calFolder'
must use AppointmentItems as the default type.");
}

// Check for duplicate entries. Helps prevent double-ups on
event listeners.
if (m_folderPaths.Contains(m_calFolder.FolderPath) == false)
{
Items items = m_calFolder.Items;
m_folderPaths.Add(m_calFolder.FolderPath);

Items delItems = m_deletedItemsFolder.Items;

// Storing a reference to the folder and to the items
collection
// keeps folder alive. This keeps the ref count up and
prevents
// the problem of intermittent release of the COM object
due
to
// garbage collection, which in turn causes events to NOT
fire.
m_calendarFolders.Add(m_calFolder);
m_calendarItems.Add(items);
m_deletedItems.Add(delItems);

// Now add event listeners.
items.ItemChange +=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
items.ItemAdd +=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
delItems.ItemAdd +=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void UnhookCalendarEvents()
{
foreach (Items curItem in m_calendarItems)
{
curItem.ItemChange -=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
curItem.ItemAdd -=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
}
foreach (Items curItem in m_deletedItems)
{
curItem.ItemAdd -=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void Calendar_ItemAdd(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentAdded != null)
{
// Notify "Add" event listener.
this.AppointmentAdded(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemChange(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentModified != null)
{
// Notify "Modify" event listener.
this.AppointmentModified(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemDelete(object item)
{
if (item is AppointmentItem)
{
if (this.AppointmentDeleting != null)
{
CancelEventArgsAppointmentItem args =
new
CancelEventArgsAppointmentItem((AppointmentItem) item);
this.AppointmentDeleting(this, args);
}
}
}
} // End class CalendarMonitor

public class EventArgsT : EventArgs
{
private T m_value;

public EventArgs(T aValue)
{
m_value = aValue;
}

public T Value
{
get { return m_value; }
set { m_value = value; }
}
}




pkelley April 14th 09 05:02 PM

Why does Outlook 2007 generate 3 events for 1 single "Add"?
 
Just seems strang that within 15 seconds of the initial ADD, I get two
modifications to the item. I guess if the modification to the item is due to
some time-stamp being posted by Exchange, I can understand that. It would
make sense.

"Dmitry Streblechenko" wrote:

Why are you suprised? Something modifies an item after it is created...
--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"pkelley" wrote in message
...
Using Windows XP, running Outlook 2007, C#, Visual Studio 2005 with VSTO
SE.
I have an Outlook AddIn that monitors calendar Appointments.
The AddIn is working great, but I've noticed a strange anomoly:

If I add a NEW Appointment, I expect to that my "Calendar_ItemAdd" event
handler will fire (only once).
If I change an EXISTING Appointment, I expect that my
"Calendar_ItemChange" event handler will fire (only once).

Real Behavior:
If I add a NEW Appointment, my "Calendar_ItemAdd" event handler fires
(as
expected), but then my "Calendar_ItemChange" event fires 15 seconds later,
and then my "Calendar_ItemChange" event fires AGAIN, another 15 seconds
later.

I'm confused. Why is Outlook 2007 calling my "Calendar_ItemChange" event
handler, when I create a single, NEW Appointment?

Here's the code that monitors the "OlDefaultFolders.olFolderCalendar"
folder:

public class CalendarMonitor
{
private NameSpace m_session;
private Liststring m_folderPaths;
private ListMAPIFolder m_calendarFolders;
private ListItems m_calendarItems;
private ListItems m_deletedItems;
private MAPIFolder m_deletedItemsFolder;
private MAPIFolder m_calFolder;
public event EventHandlerEventArgsAppointmentItem
AppointmentAdded;
public event EventHandlerEventArgsAppointmentItem
AppointmentModified;
public event EventHandlerCancelEventArgsAppointmentItem
AppointmentDeleting;

// Constructor
public CalendarMonitor(NameSpace session)
{
m_folderPaths = new Liststring();
m_calendarFolders = new ListMAPIFolder();
m_calendarItems = new ListItems();
m_deletedItems = new ListItems();
m_session = session;
m_deletedItemsFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder DeletedItems);
m_calFolder =
session.GetDefaultFolder(OlDefaultFolders.olFolder Calendar);
HookupDefaultCalendarEvents();
} // End constructor CalendarMonitor()

public void Shutdown()
{
UnhookCalendarEvents();
m_folderPaths.Clear();
m_folderPaths = null;
m_calendarFolders.Clear();
m_calendarFolders = null;
m_calendarItems.Clear();
m_calendarItems = null;
m_deletedItems.Clear();
m_deletedItems = null;
m_session = null;
m_deletedItemsFolder = null;
m_calFolder = null;
AppointmentAdded = null;
AppointmentModified = null;
AppointmentDeleting = null;
} // End Shutdown()

private void HookupDefaultCalendarEvents()
{
if ((m_calFolder != null) && (m_deletedItemsFolder != null))
{
HookupCalendarEvents();
}
} // End HookupDefaultCalendarEvents()

private void HookupCalendarEvents()
{
if (m_calFolder.DefaultItemType !=
OlItemType.olAppointmentItem)
{
throw new ArgumentException("The MAPIFolder 'm_calFolder'
must use AppointmentItems as the default type.");
}

// Check for duplicate entries. Helps prevent double-ups on
event listeners.
if (m_folderPaths.Contains(m_calFolder.FolderPath) == false)
{
Items items = m_calFolder.Items;
m_folderPaths.Add(m_calFolder.FolderPath);

Items delItems = m_deletedItemsFolder.Items;

// Storing a reference to the folder and to the items
collection
// keeps folder alive. This keeps the ref count up and
prevents
// the problem of intermittent release of the COM object
due
to
// garbage collection, which in turn causes events to NOT
fire.
m_calendarFolders.Add(m_calFolder);
m_calendarItems.Add(items);
m_deletedItems.Add(delItems);

// Now add event listeners.
items.ItemChange +=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
items.ItemAdd +=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
delItems.ItemAdd +=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void UnhookCalendarEvents()
{
foreach (Items curItem in m_calendarItems)
{
curItem.ItemChange -=
new
ItemsEvents_ItemChangeEventHandler(Calendar_ItemCh ange);
curItem.ItemAdd -=
new ItemsEvents_ItemAddEventHandler(Calendar_ItemAdd);
}
foreach (Items curItem in m_deletedItems)
{
curItem.ItemAdd -=
new
ItemsEvents_ItemAddEventHandler(Calendar_ItemDelet e);
}
}

private void Calendar_ItemAdd(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentAdded != null)
{
// Notify "Add" event listener.
this.AppointmentAdded(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemChange(object item)
{
if (item is AppointmentItem)
{
// Verify that our event handler is defined.
if (this.AppointmentModified != null)
{
// Notify "Modify" event listener.
this.AppointmentModified(this,
new
EventArgsAppointmentItem((AppointmentItem)item)) ;
}
}
}

private void Calendar_ItemDelete(object item)
{
if (item is AppointmentItem)
{
if (this.AppointmentDeleting != null)
{
CancelEventArgsAppointmentItem args =
new
CancelEventArgsAppointmentItem((AppointmentItem) item);
this.AppointmentDeleting(this, args);
}
}
}
} // End class CalendarMonitor

public class EventArgsT : EventArgs
{
private T m_value;

public EventArgs(T aValue)
{
m_value = aValue;
}

public T Value
{
get { return m_value; }
set { m_value = value; }
}
}





Ken Slovak - [MVP - Outlook] April 15th 09 02:15 PM

Why does Outlook 2007 generate 3 events for 1 single "Add"?
 
I'm not going to even try analyzing all that code.

ItemAdd() provides you with an item, as does ItemChange(), ItemRemove() does
not. If what you were doing was adding an ItemAdd() handler for Deleted
Items then I misread your code, if it was an ItemRemove() event handler no
item would be supplied.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"pkelley" wrote in message
...
With regard to Delete:

In the constructor method "CalendarMonitor" I get a handle to the deleted
items folder.
Then in "HookupCalendarEvents()" I'm trying to handle the ItemAdd event
of
the deleted
items folder so whenever an item is moved to the deleted items folder the
"Calendar_ItemDelete" event
handler fires and "Calendar_ItemDelete" is passed the item added to the
delete folder.

Also, in the constructor "CalendarMonitor" I get a handle to the calendar
folder.
Then in "HookupCalendarEvents()" I handle the ItemAdd and ItemChange
events
of the calendar
folder.

In all three cases, Calendar_ItemChange, Calendar_ItemAdd,
Calendar_ItemDelete seem to receive
the proper Item for processing. Do you still see something I'm missing?
If
yes, what would you
do to handle the ItemAdd event to the deleted items folder?


With regard to the ItemAdd Event, the Event Handlers are in my "ThisAddIn"
class:

public partial class ThisAddIn
{
#region Instance Variables

private Outlook.Inspectors m_Inspectors; // Outlook
inspectors collection
private Outlook.NameSpace m_nameSpace;
internal static ListOutlookInspector m_Windows; // List of
tracked
inspector windows
internal static Office.IRibbonUI m_Ribbon; // Ribbon UI
reference
private CalendarMonitor m_monitor; // Calendar
appointment events.
private Outlook.AppointmentItem m_olAppointment;

#endregion

#region VSTO Startup and Shutdown methods

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Initialize variables
m_Inspectors = this.Application.Inspectors;
m_nameSpace = this.Application.GetNamespace("MAPI");
m_Windows = new ListOutlookInspector();
m_monitor = new CalendarMonitor(this.Application.Session);

// Wire up event handlers
m_monitor.AppointmentAdded +=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentAdded);
m_monitor.AppointmentDeleting +=
new
EventHandlerCancelEventArgsMicrosoft.Office.Inte rop.Outlook.AppointmentItem(Monitor_AppointmentD eleting);
m_monitor.AppointmentModified +=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentModifie d);
m_Inspectors.NewInspector +=
new
Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Unhook event handlers
if (m_odbcForm != null)
{
m_odbcForm.ODBCConnectionStatus -=
new
EventHandlerEventArgsbool(SetODBCConnectionSta tus);
}
m_monitor.AppointmentAdded -=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentAdded);
m_monitor.AppointmentDeleting -=
new
EventHandlerCancelEventArgsMicrosoft.Office.Inte rop.Outlook.AppointmentItem(Monitor_AppointmentD eleting);
m_monitor.AppointmentModified -=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentModifie d);
m_monitor.Shutdown();
m_odbcForm.FormShutdown();
m_Inspectors.NewInspector -= new
Microsoft.Office.Interop.Outlook.InspectorsEvents_ NewInspectorEventHandler(Inspectors_NewInspector);

// Dereference objects
m_Inspectors = null;
m_Windows.Clear();
m_Windows = null;
m_Ribbon = null;
m_nameSpace = null;
m_monitor = null;
m_olAppointment = null;
}

#endregion

#region Methods

/// summary
/// Looks up the window wrapper for a given window object
/// /summary
/// param name="window"An outlook inspector window/param
/// returns/returns
internal static OutlookInspector FindOutlookInspector(object
window)
{
foreach (OutlookInspector inspector in m_Windows)
{
if (inspector.Window == window)
{
return inspector;
}
}
return null;
}

/// summary
/// Iterates through a list of Appointment "Outlook.Recipients" and
/// has Microsoft Exchange identify their SMTP email addresses.
/// This routine then concatenates the email address to the end of
a
/// string and separates each email address using a semi-colon ";"
/// followed by a space " " character.
/// The returned string is trimmed of spaces (and tabs) leaving a
/// string containing email addresses separated and terminated by
/// a semi-colon ";"
/// /summary
/// param name="recipients"A collection of Recipient
objects./param
/// returnsA string object containing Appointment Attendee email
addresses.
/// For Example:
/// ; ;
;"
/// /returns
private string
GetAttendees(Microsoft.Office.Interop.Outlook.Reci pients recipients)
{
string attendees = null;
Microsoft.Office.Interop.Outlook.ExchangeUser exUser = null;
string smtpAddr = null;
foreach (Microsoft.Office.Interop.Outlook.Recipient recipient
in
recipients)
{
exUser = recipient.AddressEntry.GetExchangeUser();
if (exUser == null)
{
smtpAddr = recipient.Address;
}
else
{
smtpAddr = exUser.PrimarySmtpAddress;
}
attendees += (smtpAddr + "; ");
}
if (attendees != null)
return attendees.Trim();
else
return ;";
}

private string GetFromEMailAddress(string fromName)
{
string emailAddr = ";
Outlook.Recipient recip =
m_nameSpace.CreateRecipient(fromName);
Outlook.ExchangeUser exchgUser =
recip.AddressEntry.GetExchangeUser();
if (exchgUser != null)
{
emailAddr = exchgUser.PrimarySmtpAddress;
}
return emailAddr;
}

#endregion

#region Event Handlers

private void SetODBCConnectionStatus(object sender, EventArgsbool
connected)
{
m_odbcIsActive = connected.Value;
if (m_ribbon != null)
{
m_ribbon.SetConnectedToDB(m_odbcIsActive);
}
}

private void Monitor_AppointmentAdded(object sender,
EventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "NEW";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. ADD to Cache ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}
}

private void Monitor_AppointmentDeleting(object sender,
CancelEventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "DELETE";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. DELETE from Cache ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}

// We could prevent the delete from happening if we wanted to,
// but we don't so allow the delete to occur.
appt.Cancel = false;
}

/// summary
/// This method is called immediately following the "Add" event of
/// a Calendar AppointmentItem.
/// This method is also called immediately following the
"Modification"
/// event of a Calendar AppointmentItem.
/// /summary
/// param name="sender"The sending object/param
/// param name="appt"The calendar appointment record
modified/param
private void Monitor_AppointmentModified(object sender,
EventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "CHANGE";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. CHANGE Cache value ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}
}

/// summary
/// The NewInspector event fires whenever a new inspector is
displayed. We use
/// this event to add our custom button to appointment item
inspectors.
/// /summary
/// param name="Inspector"/param
private void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
try
{
// Debug.WriteLine("Inspectors_NewInspector");
OutlookItem olItem = new
OutlookItem(Inspector.CurrentItem);

// Make sure this is an appointment item
if (olItem.Class == Outlook.OlObjectClass.olAppointment)
{
m_olAppointment =
(Outlook.AppointmentItem)Inspector.CurrentItem;
m_ribbon.setApptItem(m_olAppointment);

// Check to see if this is a new window
// we don't already track
OutlookInspector existingWindow =
FindOutlookInspector(Inspector);
// If the m_Windows collection does not
// have a window for this Inspector,
// we should add it to m_Windows
if (existingWindow == null)
{
OutlookInspector window = new
OutlookInspector(Inspector);
window.Close += new
EventHandler(WrappedWindow_Close);
window.InvalidateControl +=
new
EventHandlerEventArgsstring(WrappedWindow_Inva lidateControl);
m_Windows.Add(window);
}

}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}

void WrappedWindow_InvalidateControl(object sender,
EventArgsstring e)
{
if (m_Ribbon != null)
{
m_Ribbon.InvalidateControl(e.Value);
}
}

void WrappedWindow_Close(object sender, EventArgs e)
{
OutlookInspector window = (OutlookInspector)sender;
window.Close -= new EventHandler(WrappedWindow_Close);
m_Windows.Remove(window);
}

#endregion

#region VSTO generated code

/// summary
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// /summary
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}



pkelley April 15th 09 03:47 PM

Why does Outlook 2007 generate 3 events for 1 single "Add"?
 
Sorry for overwhelming you with too much code. I get a little intense
sometimes.

I was trying to answer your question, "What exactly is being done when
ItemAdd fires, are you doing anything with that item and its properties?"
The code shows it all.

Anyway, thank you for spending some time on this. You have been helpful.

"Ken Slovak - [MVP - Outlook]" wrote:

I'm not going to even try analyzing all that code.

ItemAdd() provides you with an item, as does ItemChange(), ItemRemove() does
not. If what you were doing was adding an ItemAdd() handler for Deleted
Items then I misread your code, if it was an ItemRemove() event handler no
item would be supplied.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"pkelley" wrote in message
...
With regard to Delete:

In the constructor method "CalendarMonitor" I get a handle to the deleted
items folder.
Then in "HookupCalendarEvents()" I'm trying to handle the ItemAdd event
of
the deleted
items folder so whenever an item is moved to the deleted items folder the
"Calendar_ItemDelete" event
handler fires and "Calendar_ItemDelete" is passed the item added to the
delete folder.

Also, in the constructor "CalendarMonitor" I get a handle to the calendar
folder.
Then in "HookupCalendarEvents()" I handle the ItemAdd and ItemChange
events
of the calendar
folder.

In all three cases, Calendar_ItemChange, Calendar_ItemAdd,
Calendar_ItemDelete seem to receive
the proper Item for processing. Do you still see something I'm missing?
If
yes, what would you
do to handle the ItemAdd event to the deleted items folder?


With regard to the ItemAdd Event, the Event Handlers are in my "ThisAddIn"
class:

public partial class ThisAddIn
{
#region Instance Variables

private Outlook.Inspectors m_Inspectors; // Outlook
inspectors collection
private Outlook.NameSpace m_nameSpace;
internal static ListOutlookInspector m_Windows; // List of
tracked
inspector windows
internal static Office.IRibbonUI m_Ribbon; // Ribbon UI
reference
private CalendarMonitor m_monitor; // Calendar
appointment events.
private Outlook.AppointmentItem m_olAppointment;

#endregion

#region VSTO Startup and Shutdown methods

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Initialize variables
m_Inspectors = this.Application.Inspectors;
m_nameSpace = this.Application.GetNamespace("MAPI");
m_Windows = new ListOutlookInspector();
m_monitor = new CalendarMonitor(this.Application.Session);

// Wire up event handlers
m_monitor.AppointmentAdded +=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentAdded);
m_monitor.AppointmentDeleting +=
new
EventHandlerCancelEventArgsMicrosoft.Office.Inte rop.Outlook.AppointmentItem(Monitor_AppointmentD eleting);
m_monitor.AppointmentModified +=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentModifie d);
m_Inspectors.NewInspector +=
new
Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Unhook event handlers
if (m_odbcForm != null)
{
m_odbcForm.ODBCConnectionStatus -=
new
EventHandlerEventArgsbool(SetODBCConnectionSta tus);
}
m_monitor.AppointmentAdded -=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentAdded);
m_monitor.AppointmentDeleting -=
new
EventHandlerCancelEventArgsMicrosoft.Office.Inte rop.Outlook.AppointmentItem(Monitor_AppointmentD eleting);
m_monitor.AppointmentModified -=
new
EventHandlerEventArgsMicrosoft.Office.Interop.Ou tlook.AppointmentItem(Monitor_AppointmentModifie d);
m_monitor.Shutdown();
m_odbcForm.FormShutdown();
m_Inspectors.NewInspector -= new
Microsoft.Office.Interop.Outlook.InspectorsEvents_ NewInspectorEventHandler(Inspectors_NewInspector);

// Dereference objects
m_Inspectors = null;
m_Windows.Clear();
m_Windows = null;
m_Ribbon = null;
m_nameSpace = null;
m_monitor = null;
m_olAppointment = null;
}

#endregion

#region Methods

/// summary
/// Looks up the window wrapper for a given window object
/// /summary
/// param name="window"An outlook inspector window/param
/// returns/returns
internal static OutlookInspector FindOutlookInspector(object
window)
{
foreach (OutlookInspector inspector in m_Windows)
{
if (inspector.Window == window)
{
return inspector;
}
}
return null;
}

/// summary
/// Iterates through a list of Appointment "Outlook.Recipients" and
/// has Microsoft Exchange identify their SMTP email addresses.
/// This routine then concatenates the email address to the end of
a
/// string and separates each email address using a semi-colon ";"
/// followed by a space " " character.
/// The returned string is trimmed of spaces (and tabs) leaving a
/// string containing email addresses separated and terminated by
/// a semi-colon ";"
/// /summary
/// param name="recipients"A collection of Recipient
objects./param
/// returnsA string object containing Appointment Attendee email
addresses.
/// For Example:
/// ; ;
;"
/// /returns
private string
GetAttendees(Microsoft.Office.Interop.Outlook.Reci pients recipients)
{
string attendees = null;
Microsoft.Office.Interop.Outlook.ExchangeUser exUser = null;
string smtpAddr = null;
foreach (Microsoft.Office.Interop.Outlook.Recipient recipient
in
recipients)
{
exUser = recipient.AddressEntry.GetExchangeUser();
if (exUser == null)
{
smtpAddr = recipient.Address;
}
else
{
smtpAddr = exUser.PrimarySmtpAddress;
}
attendees += (smtpAddr + "; ");
}
if (attendees != null)
return attendees.Trim();
else
return ;";
}

private string GetFromEMailAddress(string fromName)
{
string emailAddr = ";
Outlook.Recipient recip =
m_nameSpace.CreateRecipient(fromName);
Outlook.ExchangeUser exchgUser =
recip.AddressEntry.GetExchangeUser();
if (exchgUser != null)
{
emailAddr = exchgUser.PrimarySmtpAddress;
}
return emailAddr;
}

#endregion

#region Event Handlers

private void SetODBCConnectionStatus(object sender, EventArgsbool
connected)
{
m_odbcIsActive = connected.Value;
if (m_ribbon != null)
{
m_ribbon.SetConnectedToDB(m_odbcIsActive);
}
}

private void Monitor_AppointmentAdded(object sender,
EventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "NEW";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. ADD to Cache ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}
}

private void Monitor_AppointmentDeleting(object sender,
CancelEventArgsOutlook.AppointmentItem appt)
{
string formatStr;
string msg;

if (m_ribbon.isDTIAssignment())
{
if ((appt.Value.Subject != null) && (appt.Value.Location !=
null))
{
string dbAction = "DELETE";
string attendees = GetAttendees(appt.Value.Recipients);
string fromEMailAddr =
GetFromEMailAddress(appt.Value.Organizer);
m_odbcForm.Do_eBudgetSQL(dbAction,
appt.Value.Subject, appt.Value.Location,
fromEMailAddr,
attendees, appt.Value.StartUTC.ToString(),
appt.Value.EndUTC.ToString(),
appt.Value.Body);
}
else
{
formatStr = "DTI Appointments require Subject and
Location. DELETE from Cache ignored.";
msg = System.String.Format(formatStr,
appt.Value.Subject);
MessageBox.Show(msg);
}
}

// We could prevent the delete from happening if we wanted to,
// but we don't so allow the delete to occur.
appt.Cancel = false;
}

/// summary
/// This method is called immediately following the "Add" event of
/// a Calendar AppointmentItem.
/// This method is also called immediately following the
"Modification"
/// event of a Calendar AppointmentItem.
/// /summary
/// param name="sender"The sending object/param
/// param name="appt"The calendar appointment record
modified/param



All times are GMT +1. The time now is 12:31 PM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2006 OutlookBanter.com