Introduction
It might not be rocket science to create appointments in Microsoft Outlook but nevertheless, doing it in C# code cost me more effort than it should have. Anyway, here is a very simple example of how to create yearly recurring reminders, which I use to create reminders of birthdays for my family.
Doing this in C# was of course not easier than just using the Outlook user interface. Writing code for the sake of it is not very productive. So, why write code like this? Well, 2 reasons:
- Because examples in this subject domain are relatively scarce. The only example I could find in MSDN was one in Java, which wasn't very well written either.
- I have a different application which integrates with Outlook using VB6 and I wanted to see how easy it is in C#.
The Code
static void Main(string[] args)
{
Outlook._Application olApp =
(Outlook._Application) new Outlook.Application();
Outlook.NameSpace mapiNS = olApp.GetNamespace("MAPI");
string profile = "";
mapiNS.Logon(profile, null, null, null);
try
{
CreateYearlyAppointment(olApp, "Birthday", "Kim",
new DateTime(2004, 03,08, 7, 0, 0));
CreateYearlyAppointment(olApp, "Birthday", "Lorraine",
new DateTime(2004, 02,21, 7, 0, 0));
CreateYearlyAppointment(olApp, "Birthday",
"Evellyn", new DateTime(2004, 05,29, 7, 0, 0));
CreateYearlyAppointment(olApp, "Birthday",
"Mum", new DateTime(2004, 12,13, 7, 0, 0));
CreateYearlyAppointment(olApp, "Tax",
"Quarterly tax bill", new DateTime(2004, 03,02, 7, 0, 0));
}
catch(Exception ex)
{
MessageBox.Show( ex.Message + " : " + ex.StackTrace);
}
MessageBox.Show("Appointments Created - toodle pip!");
return;
}
static void CreateYearlyAppointment(Outlook._Application olApp,
string reminderComment, string person, DateTime dt)
{
Outlook._AppointmentItem apt = (Outlook._AppointmentItem)
olApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
apt.Subject = person + " : " + reminderComment;
apt.Body = reminderComment;
apt.Start = dt;
apt.End = dt.AddHours(1);
apt.ReminderMinutesBeforeStart = 24*60*7 * 1;
apt.BusyStatus = Outlook.OlBusyStatus.olTentative;
apt.AllDayEvent = false;
apt.Location = "";
Outlook.RecurrencePattern myPattern = apt.GetRecurrencePattern();
myPattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursYearly;
myPattern.Interval = 1;
apt.Save();
}
Conclusion
Never forget your Mums birthday again!