Udostępnij za pośrednictwem


Issue: Unable to set recurrence pattern of an appointment or a meeting as every weekday using Outlook Object Model

The only available recurrence patterns to set from OOM are as below:

olRecursDaily = 0,

olRecursWeekly = 1,

olRecursMonthly = 2,

olRecursMonthNth = 3,

olRecursYearly = 5,

and olRecursYearNth = 6.

Though we can set every weekday recurrence pattern using UI, Outlook object model does not expose any method to create the pattern as Every Weekday.

So, We could use "DayOfWeekMask" to workaround the issue;  We found that the mask value is set to 62 on item when we set the recurrence pattern as Every Weekday through Outlook UI [Daily] -> [Every weekday].

Here is the sample code snippet in VBA and C# to workaround the issue:

NOTE: Following programming examples is for illustration only, without warranty either expressed or implied,
including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose.
This sample code assumes that you are familiar with the programming language being demonstrated and the tools used
to create and debug procedures.

SAMPLE CODE I (VBA):

    1:  Sub CreateTestAppointment()
    2:      Dim myOlApp As Outlook.Application
    3:      Dim myItem As Outlook.AppointmentItem
    4:      Dim myPattern As Outlook.RecurrencePattern
    5:      Set myOlApp = CreateObject("Outlook.Application")
    6:      Set myItem = myOlApp.CreateItem(olAppointmentItem)
    7:      Set myPattern = myItem.GetRecurrencePattern
    8:      myPattern.RecurrenceType = 1
    9:      myPattern.DayOfWeekMask = 62 
   10:      myPattern.StartTime = "12:00 pm" 'Time each appointment begins
   11:      myPattern.EndTime = "1:00 pm" 'Time each appointment ends
   12:      myPattern.PatternStartDate = #7/1/2009# 'Earliest date appt can  Occur
   13:      myPattern.PatternEndDate = #9/2/2009# 'Latest date appt can occur
   14:   
   15:      myItem.Subject = "TestAppointment"
   16:      myItem.Save
   17:      myItem.Display
   18:  End Sub

 

SAMPLE CODE II (C#):

    1:          try
    2:              {
    3:                  Outlook.Application oApp =(Outlook.Application) new Outlook.Application();
    4:                  Outlook.AppointmentItem newAppointment =(Outlook.AppointmentItem) oApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
    5:                  Outlook.RecurrencePattern oRecurr = newAppointment.GetRecurrencePattern() ;
    6:                  
    7:                  oRecurr.RecurrenceType = Outlook.OlRecurrenceType.olRecursWeekly;
    8:                  oRecurr.DayOfWeekMask = (Outlook.OlDaysOfWeek)62;
    9:                  oRecurr.StartTime = DateTime.Now.AddHours(2);
   10:                  oRecurr.EndTime = DateTime.Now.AddHours(3);
   11:                  oRecurr.PatternStartDate = DateTime.Now.Date;
   12:                  oRecurr.PatternEndDate = DateTime.Now.Date.AddDays(3);
   13:                  
   14:                  newAppointment.Body = "Test Appointment";
   15:                  newAppointment.AllDayEvent = false;
   16:                  newAppointment.Subject = "Test Appointment";
   17:                  newAppointment.Save();
   18:                  newAppointment.Display(true);
   19:              }
   20:              catch (Exception ex)
   21:              {
   22:                  MessageBox.Show("The following error occurred: " + ex.Message);
   23:              }

References:

Hope this helps.

Comments

  • Anonymous
    February 07, 2012
    With C#, .net 4.0 and Visual Studio 2010, to set DayOfWeekMask to 62,65,127 will cause running time exception. However, with normal Outlook 2010, such setup for a recurring appointment is not a issue. Just wondering if it's a bug with Visual Studio/Office.Interop.Outlook.
  • Anonymous
    February 07, 2012
    @Dean, That works fine for me in both VBA for Outlook 2010 and C# with .Net 4 out of VS 2010. Which version of OOM Lib do have reference to in your project?