Eugene Astafiev

How To: Create a new recurrent Task item in Outlook

There are two item types that can be recurrent in Outlook: appointment and task items. In one of my earlier posts described the first one – How To: Create a new recurring Outlook Appointment item. Now I am going to tell you the story about task items. Let’s go!

The Outlook Object Model provides only one method for marking Outlook items as recurrent – GetRecurrentPattern. It returns an instance of the RecurrencePattern class. This pattern represents the attributes of a recurrent item.

I’ve tried to run the code I used in my previous article for appointment items (of course, I changed the item type to the TaskItem class) and caught an unexpected exception:

Exception: The property for the recurrence type is not valid. Verify your code.

The point is that StartTime and EndTime are not applicable in case of tasks. So, please be aware that if you ever try to set these properties for task items, you will get an exception.

I admit that both items (appointment and task) allow you to set up reminders. You just need to set the ReminderSet property of the appointment or task item to true. However, there is no need to specify the time and date for the reminder in case of appointment items. The TaskItem class provides the ReminderTime property for this.

Just pass an instance of the Application class to the CreateRecurrentTaskItem method to get it running. The code below creates a new recurring task item with the “Don’t forget to buy milk! ;-)” subject and reminder set to 8:30 PM.

C# & Add-in Express:

private void CreateRecurrentTaskItem(Outlook._Application OutlookApp)
{
    Outlook.RecurrencePattern pattern = null;
    Outlook.TaskItem task = null;
    try
    {
        task = OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
        pattern = task.GetRecurrencePattern();
        pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursDaily;
        pattern.PatternStartDate = DateTime.Parse("11/02/2012");
        pattern.PatternEndDate = DateTime.Parse("11/03/2012");
        task.ReminderSet = true;
        task.ReminderTime = DateTime.Parse("8:30:00 PM");
        task.Subject = "Don't forget to buy milk! ;-)";
        task.Save();
        task.Display(false);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (pattern != null) Marshal.ReleaseComObject(pattern);
        if (task != null) Marshal.ReleaseComObject(task);
    }
}

C# & VSTO:

using System.Runtime.InteropServices;
// ...
private void CreateRecurrentTaskItem(Outlook._Application OutlookApp)
{
    Outlook.RecurrencePattern pattern = null;
    Outlook.TaskItem task = null;
    try
    {
        task = OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
        pattern = task.GetRecurrencePattern();
        pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursDaily;
        pattern.PatternStartDate = DateTime.Parse("11/02/2012");
        pattern.PatternEndDate = DateTime.Parse("11/03/2012");
        task.ReminderSet = true;
        task.ReminderTime = DateTime.Parse("8:30:00 PM");
        task.Subject = "Don't forget to buy milk! ;-)";
        task.Save();
        task.Display(false);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (pattern != null) Marshal.ReleaseComObject(pattern);
        if (task != null) Marshal.ReleaseComObject(task);
    }
}

VB.NET & Add-in Express:

Private Sub CreateRecurrentTaskItem(ByRef OutlookApp As Outlook._Application)
    Dim pattern As Outlook.RecurrencePattern = Nothing
    Dim task As Outlook.TaskItem = Nothing
    Try
        task = OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem)
        pattern = task.GetRecurrencePattern()
        pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursDaily
        pattern.PatternStartDate = DateTime.Parse("11/02/2012")
        pattern.PatternEndDate = DateTime.Parse("11/03/2012")
        task.ReminderSet = True
        task.ReminderTime = DateTime.Parse("8:30:00 PM")
        task.Subject = "Don't forget to buy milk! ;-)"
        task.Save()
        task.Display(False)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(pattern) Then Marshal.ReleaseComObject(pattern)
        If Not IsNothing(task) Then Marshal.ReleaseComObject(task)
    End Try
End Sub

VB.NET & VSTO:

Imports System.Runtime.InteropServices
' ...
Private Sub CreateRecurrentTaskItem(ByRef OutlookApp As Outlook._Application)
    Dim pattern As Outlook.RecurrencePattern = Nothing
    Dim task As Outlook.TaskItem = Nothing
    Try
        task = OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem)
        pattern = task.GetRecurrencePattern()
        pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursDaily
        pattern.PatternStartDate = DateTime.Parse("11/02/2012")
        pattern.PatternEndDate = DateTime.Parse("11/03/2012")
        task.ReminderSet = True
        task.ReminderTime = DateTime.Parse("8:30:00 PM")
        task.Subject = "Don't forget to buy milk! ;-)"
        task.Save()
        task.Display(False)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(pattern) Then Marshal.ReleaseComObject(pattern)
        If Not IsNothing(task) Then Marshal.ReleaseComObject(task)
    End Try
End Sub

That's all for today, folks. See you on our forums and in the support e-mail!

2 Comments

  • https://secure.gravatar.com/avatar/e2094f201b769b33cba879192d3a9636?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G frank says:

    How am I going to raise a question regarding the interaction of IE plug-in(built with Add-in Express) with Microsoft WCF service? Thanks.

  • https://secure.gravatar.com/avatar/bbd979ae5b772d0bc1f82065aa4ba146?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Eugene Astafiev says:

    Hi Frank,

    Thank you for your interest in our products.

    Please use our forums or the support e-mail address (see readme.txt for details).

Post a comment

Have any questions? Ask us right now!