Skip Navigation Links

SyncEvents

Description

SyncEvents programmatically saves Outlook events (appointments and meetings) to a disk file for display on another computer. SyncEvents handles recurring and non-recurring events.



SyncEvents Screen Shot

Command-Line Arguments

SyncEvents requires command-line arguments to control its behavior:

Command-Line Description Example
SyncEvents.exe /Office {file path} Save Outlook appointments and meetings to the specified file path every 15 minutes. "C:\Program Files\SyncEvents\SyncEvents.exe" /Office f:\schedule.dat
SyncEvents.exe /Home {file path} Load appointments and meetings from the specified file path. "C:\Program Files\SyncEvents\SyncEvents.exe" /Home f:\schedule.dat

The C# concepts illustrated by this source code include:

  1. Accessing Microsoft Office via COM Interop
  2. Determining if a recurring appointment or meeting occurrs on a given day

LoadEvents Source Code

private void LoadEvents()
{
  Cursor.Current = Cursors.WaitCursor;

  // Access Outlook via COM interop.
  Outlook.Application olApp = new Outlook.ApplicationClass();

  // Get folder that contains appointments and meetings.
  Outlook.MAPIFolder olFolder;
  Outlook.AppointmentItem olAppoint;
  olFolder = olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

  olAppoint = (Outlook.AppointmentItem) olFolder.Items.GetFirst();

  scheduleHashtable.Clear();

  // Save events that occur in the specified window.
  const int daysBeforeToday =  7;
  const int daysAfterToday  = 31;

  DateTime firstDay = DateTime.Now.Date.AddDays(-daysBeforeToday);
  DateTime lastDay  = DateTime.Now.Date.AddDays( daysAfterToday);

  // For each event...
  for (int i = 1; i <= olFolder.Items.Count; i++)
  {
    olAppoint = (Outlook.AppointmentItem) olFolder.Items.Item(i);

    if (!olAppoint.IsRecurring && olAppoint.Start >= DateTime.Now.Date)
    {
      // Add non-recurring event that happens today.
      AddAppointmentItem(olAppoint);
    }
    else if (olAppoint.IsRecurring)
    {
      // Iterate through the date window and 
      // add this recurring appointment to any date in that range
      // on which it recurrs.

      Outlook.RecurrencePattern recurrencePattern = olAppoint.GetRecurrencePattern();

      for (DateTime currentDate = firstDay; currentDate <= lastDay; currentDate = currentDate.AddDays(1))
      {
        DateTime dt = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, olAppoint.Start.Hour, olAppoint.Start.Minute, 0);

        Outlook.AppointmentItem recurrence = null;

        try
        {
          recurrence = recurrencePattern.GetOccurrence(dt);
        }
        catch (System.Exception)
        {
          recurrence = null;
        }

        if (recurrence != null)
        {
          AddAppointmentItem(recurrence);
        }
      }
    }
  }

  Cursor.Current = Cursors.Arrow;
}

Requirements

This source code was developed with Microsoft Visual C# .net.

Source Code



Home Page   Shareware   C# .NET Tips   Download C# .NET Source Code   Browse C# .NET Source Code   C# .NET Open Source   On-Line Utilities   Blog   Contact