ItemChange Event is modifying my calendar

Joined
Oct 18, 2006
Messages
1
Reaction score
0
I've got what I think is a wierd problem here. I am trying to display a "Daily Planner" on my desktop that shows all my appointments for the day. This little program works great with one annoying problem. If the application is running and I open a Calendar Event within Outlook itself...I don't change anything but hit the Save & Close button on the Event. When the Event is saved, it now has a Weekly Recurrance set on the event. All Events that are modified will now have the weekly recurrance set if my little application is running. If I close my "Daily Planner", this symptom goes away completely.

Here is the entire class OutlookCalendar that I'm using. The Main Form sets the Delegate AppointmentsModified which re-loads all the appointments for Today (by calling the method getCalendarDataSet) and re-displays them.

Any and all help would be greatly appreciated.

Mark

---------------------------------------------

Code:
using System;
using System.Data;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace DailyPlanner
{
/// 
/// Summary description for OutlookCalendar.
/// 
public class OutlookCalendar : IDisposable
{
	 public delegate void AppointmentsModifiedDelegate();
	 public event AppointmentsModifiedDelegate AppointmentsModified;
	 private Outlook.Application objOutlook = null;
	 private Outlook.NameSpace objNamespace = null;
	 private Outlook.MAPIFolder objFolder = null;
	 Outlook.Items colCal = null;
 
	 public OutlookCalendar()
	 {
		 this.objOutlook = new Outlook.ApplicationClass();
		 this.objNamespace = this.objOutlook.GetNamespace("MAPI");
		 this.objFolder = this.objNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
		 this.colCal = this.objFolder.Items;
		 this.colCal.Sort("[Start]", false);
		 this.colCal.IncludeRecurrences = true;
		 this.colCal.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(colCal_ItemAdd);
		 this.colCal.ItemChange += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(colCal_ItemChange);
		 this.colCal.ItemRemove += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemRemoveEventHandler(colCal_ItemRemove);
	 }
 
	 /// 
	 /// Close the Outlook application when this instance is disposed.
	 /// 
	 public void Dispose()
	 {
		 if (objOutlook != null) 
		 {
			this.objOutlook = null;
			this.objNamespace = null;
			this.objFolder = null;
			this.colCal = null;
		 }
	 }
 
	 /// 
	 /// Retrieve a list of all appointments listed in the Outlook calendar.
	 /// 
	 /// Calendar Items DataSet
	 public DataTable getCalendarDataSet(DateTime dteDate)
	 {
		 DataColumn col;
		 Outlook.Items thisDayItems;
		 DataTable rv = new DataTable("Appointment");
		 rv.Columns.Add("EventID");
		 rv.Columns.Add("Category");
		 col = new DataColumn("Start");
		 col.DataType = System.Type.GetType("System.DateTime");
		 rv.Columns.Add(col);
		 col = new DataColumn("End");
		 col.DataType = System.Type.GetType("System.DateTime");
		 rv.Columns.Add(col);
		 rv.Columns.Add("Subject");
		 col = new DataColumn("IsRepeat");
		 col.DataType = System.Type.GetType("System.Boolean");
		 rv.Columns.Add(col);
		 rv.Columns.Add("Repeat");
		 col = new DataColumn("DaysOfWeekMask");
		 col.DataType = System.Type.GetType("System.Int32");
		 rv.Columns.Add(col);
		 col = new DataColumn("RepeatUntil");
		 col.DataType = System.Type.GetType("System.DateTime");
		 rv.Columns.Add(col);
		 col = new DataColumn("Interval");
		 col.DataType = System.Type.GetType("System.Int32");
		 rv.Columns.Add(col);
		 rv.Columns.Add("Location");
		 col = new DataColumn("AllDayEvent");
		 col.DataType = System.Type.GetType("System.Boolean");
		 rv.Columns.Add(col);
		 rv.Columns.Add("Duration");
		 rv.Columns.Add("Organizer");
		 rv.Columns.Add("Importance");
		 rv.Columns.Add("Sensitivity");
		 rv.Columns.Add("Body");
		 try
		 {
			string nowdate = dteDate.ToString("MM/dd/yyyy");
			string tomorrowDate = dteDate.AddDays(1).ToString("MM/dd/yyyy");
			string query = "[Start] >= '" + nowdate + " 00:00 AM' and [End] <= '" + tomorrowDate + " 00:00 AM'";
			Debug.WriteLine(query);
			thisDayItems = colCal.Restrict(query);
			foreach(Outlook.AppointmentItem item in thisDayItems)
			{
			 string itemID = item.EntryID;
			 string desc = item.Subject;
			 DateTime dtStart = item.Start;
			 DateTime dtEnd = item.End;
			 Outlook.RecurrencePattern recur = item.GetRecurrencePattern();
			 if(item.AllDayEvent)
				 dtEnd = dtEnd.AddSeconds(-1); //Subtract one second from the end date if this is an all day event
				 rv.Rows.Add(new object[]
				 {
					 itemID,
					 item.Categories,
					 dtStart,
					 dtEnd,
					 item.Subject,
					 item.IsRecurring,
					 recur.RecurrenceType,
					 recur.DayOfWeekMask,
					 recur.PatternEndDate,
					 recur.Interval,
					 item.Location,
					 item.AllDayEvent,
					 item.Duration,
					 item.Organizer,
					 item.Importance,
					 item.Sensitivity,
					 item.Body
				 });
			}
			Debug.WriteLine(rv.Rows.Count + " Appointments exported.");
		 }
 
		 catch (System.Exception e)
		 {
			Console.WriteLine(e);
		 }
 
		 return rv;
	 }
 
	 public void ShowEntry(string entryID)
	 {
		 objFolder = objNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
		 Outlook.AppointmentItem item = (Outlook.AppointmentItem)objNamespace.GetItemFromID(entryID, objFolder.StoreID);
		 if(item != null)
			item.Display(false);
	 }
 
	 private void colCal_ItemAdd(object Item)
	 {
		 if(this.AppointmentsModified != null)
			this.AppointmentsModified();
	 }
 
	 private void colCal_ItemChange(object Item)
	 {
		 if(this.AppointmentsModified != null)
			this.AppointmentsModified();
	 }
 
	 private void colCal_ItemRemove()
	 {
		 if(this.AppointmentsModified != null)
			this.AppointmentsModified();
	 }
}
}
 
Top