How To: Create a new Task item in Outlook
In my previous posts I have described how to create a new e-mail message, contact and appointment items. Today I am going to cover task items.
Let’s start from the easiest one. The Application class of the Outlook Object Model provides you with the CreateItem method which allows creating various item types. And one of them is the task item. You just need to pass the olTaskItem constant to the CreateItem method to get the job done.
C# & Add-in Express:
private void CreateTaskItemUsingCreateItem(Outlook._Application OutlookApp) { Outlook.TaskItem task = OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem; if (task != null) { task.Subject = "change oil in the car"; task.Save(); task.Display(false); Marshal.ReleaseComObject(task); } }
C# & VSTO:
using System.Runtime.InteropServices; // ... private void CreateTaskItemUsingCreateItem(Outlook._Application Application) { Outlook.TaskItem task = Application.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem; if (task != null) { task.Subject = "change oil in the car"; task.Save(); task.Display(false); Marshal.ReleaseComObject(task); } }
VB.NET & Add-in Express:
Private Sub CreateTaskItemUsingCreateItem(ByRef OutlookApp As Outlook._Application) Dim task As Outlook.TaskItem = OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem) If Not IsNothing(task) Then task.Subject = "change oil in the car" task.Save() task.Display(False) Marshal.ReleaseComObject(task) End If End Sub
VB.NET & VSTO:
Imports System.Runtime.InteropServices ' ... Private Sub CreateTaskItemUsingCreateItem(ByRef Application As Outlook._Application) Dim task As Outlook.TaskItem = Application.CreateItem(Outlook.OlItemType.olTaskItem) If Not IsNothing(task) Then task.Subject = "change oil in the car" task.Save() task.Display(False) Marshal.ReleaseComObject(task) End If End Sub
You may consider one more way to create a task item by using the Add method of the Items class (which comes from the Task’s default folder). This method also accepts the OlItemType enumeration and returns a newly created Outlook item.
C# & Add-in Express:
private void CreateTaskItemUsingItemAdd(Outlook._Application OutlookApp) { Outlook.NameSpace ns = null; Outlook.MAPIFolder taskFolder = null; Outlook.Items items = null; Outlook.TaskItem task = null; try { ns = OutlookApp.GetNamespace("MAPI"); taskFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks); items = taskFolder.Items; task = items.Add(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem; task.Subject = "change oil in the car"; task.Save(); task.Display(false); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (task != null) Marshal.ReleaseComObject(task); if (items != null) Marshal.ReleaseComObject(items); if (taskFolder != null) Marshal.ReleaseComObject(taskFolder); if (ns != null) Marshal.ReleaseComObject(ns); } }
C# & VSTO:
using System.Runtime.InteropServices; // ... private void CreateTaskItemUsingItemAdd(Outlook._Application Application) { Outlook.NameSpace ns = null; Outlook.MAPIFolder taskFolder = null; Outlook.Items items = null; Outlook.TaskItem task = null; try { ns = Application.GetNamespace("MAPI"); taskFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks); items = taskFolder.Items; task = items.Add(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem; task.Subject = "change oil in the car"; task.Save(); task.Display(false); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (task != null) Marshal.ReleaseComObject(task); if (items != null) Marshal.ReleaseComObject(items); if (taskFolder != null) Marshal.ReleaseComObject(taskFolder); if (ns != null) Marshal.ReleaseComObject(ns); } }
VB.NET & Add-in Express:
Private Sub CreateTaskItemUsingItemAdd(ByRef OutlookApp As Outlook._Application) Dim ns As Outlook.NameSpace = Nothing Dim taskFolder As Outlook.MAPIFolder = Nothing Dim items As Outlook.Items = Nothing Dim task As Outlook.TaskItem = Nothing Try ns = OutlookApp.GetNamespace("MAPI") taskFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks) items = taskFolder.Items task = items.Add(Outlook.OlItemType.olTaskItem) task.Subject = "change oil in the car" task.Save() task.Display(True) Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.Message) Finally If Not IsNothing(task) Then Marshal.ReleaseComObject(task) If Not IsNothing(items) Then Marshal.ReleaseComObject(items) If Not IsNothing(taskFolder) Then Marshal.ReleaseComObject(taskFolder) If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns) End Try End Sub
VB.NET & VSTO:
Imports System.Runtime.InteropServices ' ... Private Sub CreateTaskItemUsingItemAdd(ByRef Application As Outlook._Application) Dim ns As Outlook.NameSpace = Nothing Dim taskFolder As Outlook.MAPIFolder = Nothing Dim items As Outlook.Items = Nothing Dim task As Outlook.TaskItem = Nothing Try ns = Application.GetNamespace("MAPI") taskFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks) items = taskFolder.Items task = items.Add(Outlook.OlItemType.olTaskItem) task.Subject = "change oil in the car" task.Save() task.Display(False) Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.Message) Finally If Not IsNothing(task) Then Marshal.ReleaseComObject(task) If Not IsNothing(items) Then Marshal.ReleaseComObject(items) If Not IsNothing(taskFolder) Then Marshal.ReleaseComObject(taskFolder) If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns) End Try End Sub
The third way is to create a task item from a template: the Outlook Object Model provides the CreateItemFromTemplate method of the Application class. This method accepts a string which is a file-path to a template. I have tested various file formats including .txt, .rtf and etc. and only .oft and .msg were accepted by Outlook 2010 x64 which is installed on my PC. Finally, CreateItemFromTemplate returns a newly created Task item based on the specified template.
C# & Add-in Express:
private void CreateTaskItemUsingCreateItemFromTemplate(Outlook._Application OutlookApp) { Outlook.TaskItem task = null; try { task = OutlookApp.CreateItemFromTemplate("D:\\Eugene Astafiev.oft") as Outlook.TaskItem; task.Subject = "change oil in the car"; task.Save(); task.Display(false); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (task != null) Marshal.ReleaseComObject(task); } }
C# & VSTO:
using System.Runtime.InteropServices; // ... private void CreateTaskItemUsingCreateItemFromTemplate(Outlook._Application Application) { Outlook.TaskItem task = null; try { task = Application.CreateItemFromTemplate( "D:\\Eugene Astafiev.oft") as Outlook.TaskItem; task.Subject = "change oil in the car"; task.Save(); task.Display(false); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (task != null) Marshal.ReleaseComObject(task); } }
VB.NET & Add-in Express:
Private Sub CreateTaskItemUsingCreateItemFromTemplate( _ ByRef OutlookApp As Outlook._Application) Dim task As Outlook.TaskItem = Nothing Try task = OutlookApp.CreateItemFromTemplate("D:\\Eugene Astafiev.oft") task.Subject = "change oil in the car" task.Save() task.Display(False) Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.Message) Finally If Not IsNothing(task) Then Marshal.ReleaseComObject(task) End Try End Sub
VB.NET & VSTO:
Imports System.Runtime.InteropServices ' ... Private Sub CreateTaskItemUsingCreateItemFromTemplate( _ ByRef Application As Outlook._Application) Dim task As Outlook.TaskItem = Nothing Try task = Application.CreateItemFromTemplate("D:\\Eugene Astafiev.oft") task.Subject = "change oil in the car" task.Save() task.Display(False) Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.Message) Finally 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!
One Comment
Very help Eugene.
Tavi