65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 在 Microsoft Outlook 中创建生日提醒

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.88/5 (8投票s)

2004年1月5日

viewsIcon

67608

downloadIcon

1206

本文展示了如何使用 Microsoft Outlook 约会。 我使用这段代码的一个版本为我的家人设置提醒。

引言

在 Microsoft Outlook 中创建约会可能并不像登天一样困难,但无论如何,用 C# 代码实现起来比我预想的要费力。 无论如何,这里有一个非常简单的例子,说明如何创建年度重复提醒,我用它来为我的家人创建生日提醒。

当然,用 C# 实现这一点并不比直接使用 Outlook 用户界面更容易。 为了编程而编程并不是很有成效。 那么,为什么要编写这样的代码呢? 好吧,有两个原因

  1. 因为在这个领域中的例子相对较少。 我在 MSDN 中找到的唯一例子是用 Java 编写的,而且写得也不是很好。
  2. 我有一个不同的应用程序,它使用 VB6 与 Outlook 集成,我想看看用 C# 实现起来是否容易。

代码

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);

   //create an appointment in the Calendar folder
   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)
  {
   // Use the Outlook application object to create an appointment
   Outlook._AppointmentItem apt = (Outlook._AppointmentItem)
    olApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
  
   // set some properties
   apt.Subject = person + " : " + reminderComment;
   apt.Body = reminderComment;

   apt.Start = dt;
   apt.End   = dt.AddHours(1);

   apt.ReminderMinutesBeforeStart = 24*60*7 * 1;  // One week
   // Makes it appear bold in the calendar - which I like!
   apt.BusyStatus = Outlook.OlBusyStatus.olTentative;  
   apt.AllDayEvent = false;
   apt.Location = "";

   Outlook.RecurrencePattern myPattern = apt.GetRecurrencePattern();
   myPattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursYearly;
   //myPattern.Regenerate = false;
   myPattern.Interval = 1;
   apt.Save();
  }

结论

再也不要忘记你妈妈的生日了!

© . All rights reserved.