The following sections outline working with the TaskItem object, which represents a task in an Outlook folder. The TaskItem object has some unique properties and methods. You can make a TaskItem recurring by using the same objects that you used to make an AppointmentItem object recurring. The only difference is you cannot use the GetOccurrence method or the Exceptions property on recurring task items. Also, this section does not cover the TaskRequestItem since it uses the same methods and properties as the MeetingItem object. For more information about using the TaskRequestItem, refer to the Olform.hlp file on the companion CD.
The following section describes some of the properties of the TaskItem object.
The DateCompleted property is a read/write Date field that stores when a particular task was completed. You can use this property to show when a task was completed. The following code shows you how to get the DateCompleted property from one of the tasks in your personal task folder:
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oTasks = oNS.GetDefaultFolder(13) set oItems = oTasks.Items set oItem = oItems.GetFirst msgbox "The DateCompleted for " & oItem.Subject & " is " & _ oItem.DateCompleted End Sub |
The DueDate specifies the due date for the task. This is a read/write property that takes a date as its parameter. The following code shows you how to use this property in your applications. It finds the first item that is due after a certain date and then displays the information about that item in a message box.
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oTasks = oNS.GetDefaultFolder(13) set oItems = oTasks.Items set oItem = oItems.Find("[DueDate]> '5/19/98'") msgbox "The " & oItem.Subject & " is due on " & oItem.DueDate End Sub |
Outlook allows you to delegate tasks by using the Assign method (which you will learn about later in this supplement), but there are times when the Assign method will not work for delegation. For example, you cannot use the Assign method for the delegation of task items if the items are stored in a Public Folder. If you modify the Owner property of the TaskItem object, you can keep other users up to date on who the current owner of the task is, though you can't delegate the items per se. The following code is taken from the Account Tracking application, which uses the Owner property to assign an owner to a task. The code does not delegate the task so that the application works in an Exchange Server Public Folder. The code first checks to make sure the user has selected another user as the owner of the task. If the user has selected someone, the code assigns that person as the owner of the task, and it sends that person an e-mail with the task they need to work on as a shortcut back to the original TaskItem in the folder.
Sub cmdAssignTask_Click set oListBox = oDefaultPage.Controls("lstTasks") set oListBox1 = oDefaultPage.Controls("lstAssignTaskName") if oListBox.ListIndex = -1 then MsgBox "No selected account task. Please select one.", _ 48, "Assign Account Task" elseif oListBox1.Value = "" then MsgBox "No selected person to assign the task to. " & _ "Please select one.", 48, "Assign Account Task" else set oItem = oRestrictedTaskItems(oListBox.ListIndex + 1) oItem.Owner = oListBox1.Value oItem.Save 'Create a mailitem set oNotifyMail = Application.CreateItem(0) oNotifyMail.Subject = "New task for the " & item.Subject & _ " account." txtMessageText = oNameSpace.CurrentUser & _ " has assigned the following task to you." & chr(13) txtMessageText = txtMessageText & "Please click on the " & _ shortcut below to access the task." oNotifyMail.Body = txtMessageText & chr(13) & chr(13) set oAttachments = oNotifyMail.Attachments oAttachments.Add oRestrictedTaskItems( _ oListBox.ListIndex + 1), 4 oNotifyMail.To = oItem.Owner if not (oNotifyMail.Recipients.ResolveAll) then 'Recip did not resolve msgbox "One of the recipients to assign the task did " & _ "not resolve.", 48, "Assign Account Task" oNotifyMail.Display else oNotifyMail.Send end if call cmdRefreshTasks_Click end if end Sub |
The following section describes some of the methods of the TaskItem object.
The Assign method delegates a task to another user. After calling this method, you need to add a recipient to the TaskItem object. You can then use the Send method of the TaskItem object to send the delegated task to the user as a TaskRequestItem object. The Assign method will not work in an Exchange Server Public Folder, but you can make a copy of the task in your personal task folder and assign the task from there. The following code shows you how to assign a task from your personal task list:
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oTasks = oNS.GetDefaultFolder(13) set oItem = oTasks.Items.Add oItem.Assign oItem.Recipients.Add("Jane") 'Make it due a week from today oItem.DueDate = DateAdd("d",Date,7) oItem.Subject = "Finalize Development Plans" oItem.Send End Sub |
The MarkComplete method makes it very easy for your application to quickly change the status of a task to completed. When you call this method, Outlook sets the PercentComplete property to 100%, the Complete property to True, and the DateCompleted property to the current date. Before calling MarkComplete on a TaskItem object, check to see whether that TaskItem object is already complete; if it is, Outlook will return an error. The following code shows you how to use this method:
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oTasks = oNS.GetDefaultFolder(13) set oItem = oTasks.Items.Find("[Complete] = False") msgbox "The first uncompleted item is " & oItem.Subject oItem.MarkComplete msgbox "MarkComplete method has been called" msgbox "The DateCompleted is " & oItem.DateCompleted msgbox "The PercentComplete is " & oItem.PercentComplete msgbox "The Complete property is " & oItem.Complete End Sub |