![]()
The TaskItem Object is one of the objects in the Outlook Object Model hierarchy. This series of articles will explore the ways you can program with tasks.
Follow the above links to learn more about the TaskItem Object's methods, properties and events.
Creating Tasks in Default Folder
Tasks may be created in several ways. First is the default method Application.CreateItem, which creates a new task in the default Tasks folder. This function creates a new Task and returns it for use in your VBA programs.
Function CreateTask() As Outlook.TaskItem Set CreateTask = Outlook.CreateItem(olTaskItem) End Function
Create Tasks in Shared Folder
You can also create tasks in shared folders. Here is a function that creates a task in the specified user's shared Tasks folder. The TaskItem is returned to the calling procedure.
To call the function, pass in either a resolved Recipient object or the name of a user whose shared Tasks folder you want to access.
Function CreateSharedDefaultTask(recip As Variant) As Outlook.TaskItem
Dim olNS As Outlook.NameSpace
Dim fldr As Outlook.MAPIFolder
Dim tempRecip As Outlook.recipient
Set olNS = GetNS(GetOutlookApp)
Select Case TypeName(recip)
Case "Recipient"
' Recipient object already created
Set fldr = olNS.GetSharedDefaultFolder(recip, olFolderTasks)
Case "String"
' create Recipient object
Set tempRecip = olNS.CreateRecipient(recip)
Set fldr = olNS.GetSharedDefaultFolder(tempRecip, olFolderTasks)
End Select
Set CreateSharedDefaultTask = fldr.Items.Add(olTaskItem)
End Function
Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
Set GetNS = app.GetNamespace("MAPI")
End Function
Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
Set GetOutlookApp = Outlook.Application
End FunctionIf a Recipient object is passed to the function, the new TaskItem is created directly. If only a name is passed, a temporary Recipient object is created using the Outlook.NameSpace.CreateRecipient Method.
Create Tasks in any Tasks Folder
We can also create new tasks in any folder that can hold Task Items. This function accepts a MAPIFolder object and checks to make sure it can hold tasks. If so, it creates a new task in that folder and returns it to the calling procedure. How you create the MAPIFolder object is up to you.
Function CreateTaskAltFolder(fldr As Outlook.MAPIFolder) As Outlook.TaskItem
' add/return new task in non-default Tasks folder
If fldr.DefaultItemType = olTaskItem Then
Set CreateTaskAltFolder = fldr.Items.Add(olTaskItem)
End If
End Function