7

I'm trying to get all appointments in outlook in week range, but reoccurring appointments are not showing up.

Here is the code:

    var outlook = new Microsoft.Office.Interop.Outlook.Application();
    var calendar = outlook.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    calendar.Items.IncludeRecurrences = true;

    string filter = String.Format("[Start] >= {0} And [End] < {1}",
            DateTime.Now.Date.ToString("ddddd h:nn AMPM"),
            DateTime.Now.Date.AddDays(5).ToString("ddddd h:nn AMPM"));
    Outlook.AppointmentItem appointment;
    foreach (var item in calendar.Items.Restrict(filter))
    {
        appointment = item as Outlook.AppointmentItem;
        if (appointment != null)
        {
            MessageBox.Show(appointment.Start.ToString());
        }
    }

How do I get all of the recurring appointments that are displayed in Outlook for a week range?

Daryl
  • 18,592
  • 9
  • 78
  • 145

1 Answers1

6

You have to use recurrence pattern Put this inside your loop:

     if (item.IsRecurring)
        {
            Microsoft.Office.Interop.Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
            DateTime first = new DateTime(2011, 11, 7, item.Start.Hour, item.Start.Minute, 0);
            DateTime last = new DateTime(2011, 12, 1);
            Microsoft.Office.Interop.Outlook.AppointmentItem recur = null;



            for (DateTime cur = first; cur <= last; cur = cur.AddDays(1))
            {
                    recur = rp.GetOccurrence(cur);
                    Console.WriteLine(cur.ToLongDateString());
            }
        }

see this for more info

Community
  • 1
  • 1
user194076
  • 8,787
  • 23
  • 94
  • 154
  • 1
    That worked great except if I tried to get an appointment for a day that it wasn't scheduled I got an exception. I've wrapped the recur = rp.GetOccurrence(cur); line with a try catch, continue, and it's working great thanks! – Daryl Nov 08 '11 at 14:09
  • I've duplicated this code but keep getting "You changed one of the recurrences of this item, and this instance no longer exists. Close any open items and try again." – Mike Cheel Mar 18 '22 at 18:23
  • Apparently Outlook can't be open jeez. – Mike Cheel Mar 18 '22 at 18:24