ItemChange event is modifying my Calendar Events

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

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.

Any and all help would be greatly appreciated.

Mark


EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
Obviously your program is setting a recurrence pattern or is accessing
RecurrencePattern without checking first for IsRecurring. Without seeing
what your code is doing there's no way to say anything more.
 
Thanks for the reply.

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.

I don't think that my program is modifying anything in the event or the
recurrance pattern. Am I wrong?

Thanks again for any help.

Mark

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


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.OlDefau
ltFolders.olFolderCalendar);
this.colCal = this.objFolder.Items;
this.colCal.Sort("[Start]", false);
this.colCal.IncludeRecurrences = true;
this.colCal.ItemAdd += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemA
ddEventHandler(colCal_ItemAdd);
this.colCal.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemC
hangeEventHandler(colCal_ItemChange);
this.colCal.ItemRemove += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemR
emoveEventHandler(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.OlDefaultFol
ders.olFolderCalendar);
Outlook.AppointmentItem item =
(Outlook.AppointmentItem)objNamespace.GetItemFromI D(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();
}
}
}
 
As I mentioned, you need to check IsRecurring before accessing
item.RecurrencePattern. Just touching that property makes an appointment
recurring. You should only access that property if IsRecurring is true.
 
Ken,

Thanks so much! Once I read the words: "touching that property makes an
appointment recurring", I knew exactly what the problem was.

I now check IsRecurring before calling the GetRecurrancePattern method.
Everything works perfectly now.

Thanks again for your prompt, concise and accurate reply.

Mark
 
Back
Top