title: Slow code when connecting to Outlook calendar
body: <p>I wrote this code and I have two problems with it:</p>
  
  <ol>
  <li>Its way too slow</li>
  <li>It shows only recurring appointments in the calendar. I want it to show not just the recurring appointments from a specific date, but also all the other appointments from that date and only from the date specified.</li>
  </ol>
  
  <p>Help would be greatly appreciated.   </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Reflection;
  using Outlook = Microsoft.Office.Interop.Outlook;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          public void GetAllCalendarItems()
          {
              try
              {
                 // outlook application object
                  Outlook.Application oApp = new Outlook.Application();
                  //folder in outlook
                  Outlook.Folders OutlookFolders = oApp.Session.Folders;
  
                  //Mapi folder object
                  Outlook.MAPIFolder Folder = null;
                  // outlook exploer object
                  Outlook.Explorer explorer = null;
                  //Map subfolder object
                  Outlook.MAPIFolder SubFolder = null;
  
                  // folder int control
                  int Folderctr;
                  // sub folder control
                  int subFolderctr;                  
  
                  // initialize base objects
  
                  //date range to get appointments
                  DateTime start = DateTime.Now;
                  DateTime end = start.AddDays(5);
  
                  // loop throught the PST files added on Outlook
                  for (Folderctr = 1; Folderctr &lt;= OutlookFolders.Count; Folderctr++)
                  {
                      Folder = OutlookFolders[Folderctr];
                      Console.WriteLine("first name of folder " +Folder.Name);
                      explorer = Folder.GetExplorer();
                      //loop throught the folders in the PST files
                      for (subFolderctr = 1; subFolderctr &lt;= explorer.CurrentFolder.Folders.Count; subFolderctr++)
                      {
                          SubFolder = explorer.CurrentFolder.Folders[subFolderctr];
  
                          if (SubFolder.Name == "SCE_Calendar")
                          {
                              GetAllCalendarItems(SubFolder);    
  
                              Console.WriteLine("subfolder name of folder " + SubFolder.Name);
                          }
                      }
                  }
  
                  //close applciation
                  oApp.Quit();
                  // release com object
                  System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp);
                  oApp = null;
              }    
              //Simple error handling.
              catch (Exception e)
              {
                  Console.WriteLine("{0} Exception caught.", e);
              } 
          }
  
          public void GetAllCalendarItems(Outlook.MAPIFolder seCal)
          {
              Outlook.Items outlookCalendarItems = null; 
             //set recursion to true
              outlookCalendarItems = seCal.Items;
              outlookCalendarItems.IncludeRecurrences = true;  
              DateTime last;
              DateTime first;
  
              foreach (Outlook.AppointmentItem item in outlookCalendarItems)
              {
                  Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
                  first = new DateTime(2012, 2, 1, item.Start.Hour, item.Start.Minute, 0);
                  last = new DateTime(2012, 2, 29);
                  Outlook.AppointmentItem recur = null;
  
                  for (DateTime cur = first; cur &lt;= last; cur = cur.AddDays(1))
                  {
                      try
                      {
                          recur = rp.GetOccurrence(cur);
                          //Console.WriteLine(recur.Subject + " -&gt; " + cur.ToLongDateString());
                          Console.WriteLine("Subject: " + recur.Body);
                          Console.WriteLine("Organizer: " + recur.Organizer);
                          Console.WriteLine("Start: " + recur.Start.ToString());
                          Console.WriteLine("End: " + recur.End.ToString());
                          Console.WriteLine("Location: " + recur.Location);
                          Console.WriteLine("Recurring: " + recur.IsRecurring);
                          Console.WriteLine("-----------------------------------------");
                          Console.WriteLine(" ");
                          //Show the item to pause.
                      }
                      catch
                      { }
                  }
              }
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              GetAllCalendarItems();
          }
      }
  }
  </code></pre>