locked
Setting the Start and End of recurring appointments RRS feed

  • Question

  • Hi all,

     

    I am making use of the following code snippet to establish which appointments have changed and depending on some conditions, I would like to change the appointment start and end back to a specific date and time (the code snippet is only illustrating the point). The problem is that I have caused an endless loop (every save causes the Item_Change to fire again) . This was not the case in Outlook 2007, I was able to change the date and time without creating an endless loop.

     

    I am making use of Windows 7, Visual Studio 2010 and Outlook 2010 Version 14.0.5128.5000 (32-bit)

     

        void ThisAddIn_ItemChange(object Item)
        {
          Outlook.AppointmentItem app = null;
          Outlook.RecurrencePattern RP = null;
          Outlook.Exception RPex = null;
          Outlook.Exceptions Exceptions = null;
          int e = 0;
          Outlook.AppointmentItem Exception = null;
    
          try
          {
            app = Item as Outlook.AppointmentItem;
            if (app.IsRecurring)
            {
              RP = app.GetRecurrencePattern();
              Exceptions = RP.Exceptions;
              if (Exceptions.Count == 0)
              {
                Trace.WriteLine("No exceptions: " + app.Start.ToString());
                Trace.WriteLine(string.Format("ItemChange => Start: {0} - Subject: {1} - RecurrenceState: {2}", app.Start, app.Subject, app.RecurrenceState.ToString()));
              }
              else
              {
                for (e = 1; e <= Exceptions.Count; e++)
                {
                  RPex = Exceptions[e];
                  if (RPex.Deleted == false)
                  {
                    Exception = RPex.AppointmentItem;
                    Trace.WriteLine("Exception(" + e.ToString() + "): " + Exception.Start.ToString());
    
                    Exceptions[e].AppointmentItem.Start = new DateTime(Exceptions[e].AppointmentItem.Start.Year, Exceptions[e].AppointmentItem.Start.Month, Exceptions[e].AppointmentItem.Start.Day, app.Start.Hour, app.Start.Minute, app.Start.Second);
                    Exceptions[e].AppointmentItem.End = new DateTime(Exceptions[e].AppointmentItem.End.Year, Exceptions[e].AppointmentItem.End.Month, Exceptions[e].AppointmentItem.End.Day, app.End.Hour, app.End.Minute, app.End.Second);
                    // ENDLESS LOOP?!?!
                    Exceptions[e].AppointmentItem.Save();
    
                    NAR(Exception);
                  }
                  NAR(RPex);
                }
                NAR(Exceptions);
                NAR(RP);
              }
            }
    
          }
          catch (System.Exception m)
          {
            Trace.WriteLine(string.Format("Exception: {0} - StackTrace: {1}", m.Message, m.StackTrace));
          }
          finally
          {
            NAR(app);
            NAR(RP);
            NAR(RPex);
            NAR(Exceptions);
            NAR(Exception);
    
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
          }
        }
    
        private void NAR(object o)
        {
          try
          {
            int i = 1;
            while (i > 0)
            {
              i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
            }
          }
          catch
          {
          }
          finally
          {
            o = null;
          }
        }
    

     

    Could someone please help me with this issue?

    Many thanks in advance.

    Kind regards,


    Regards Programm3r
    Tuesday, March 1, 2011 11:27 AM

Answers

  • i am not sure I understand what your code is trying to do - do you just round down the start and end time to a second?

    Also, you code is very inefficient. You are using multiple dot notation all over, even if you do not neeed to. For example , the line

    Exceptions[e].AppointmentItem.Start = new DateTime(Exceptions[e].AppointmentItem.Start.Year, Exceptions[e].AppointmentItem.Start.Month, Exceptions[e].AppointmentItem.Start.Day, app.Start.Hour, app.Start.Minute, app.Start.Second);

    can be changed to

    DateTime start = Exception.Start;

    DateTime seriesStart = app.Start;

    Exception.Start = new DateTime(start.Year, start.Month, start.Day, seriesStart .Hour, seriesStart .Minute, seriesStart .Second);


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Wednesday, March 2, 2011 2:28 PM

All replies

  • Why not check if the values are already what you want them to be and avoid modifying the appointemnt and calling Save?
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Tuesday, March 1, 2011 5:16 PM
  • Hi Dmitry,

    Many thanks for the reply and information. Kinda figured that was the obvious answer ... so sorry about that?!

    One last thing, would this best the way to set an appointment start and end date / time within the ItemChange event. What I'm trying to get at is, am I going about this the right way?

    Many thanks again!

    Kind regards,


    Regards Programm3r
    Wednesday, March 2, 2011 5:26 AM
  • i am not sure I understand what your code is trying to do - do you just round down the start and end time to a second?

    Also, you code is very inefficient. You are using multiple dot notation all over, even if you do not neeed to. For example , the line

    Exceptions[e].AppointmentItem.Start = new DateTime(Exceptions[e].AppointmentItem.Start.Year, Exceptions[e].AppointmentItem.Start.Month, Exceptions[e].AppointmentItem.Start.Day, app.Start.Hour, app.Start.Minute, app.Start.Second);

    can be changed to

    DateTime start = Exception.Start;

    DateTime seriesStart = app.Start;

    Exception.Start = new DateTime(start.Year, start.Month, start.Day, seriesStart .Hour, seriesStart .Minute, seriesStart .Second);


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Wednesday, March 2, 2011 2:28 PM