
April 15th 09, 03:47 PM
posted to microsoft.public.outlook.program_addins
|
|
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
|