Below are some examples for using Tasks in Outlook.  Before you can use these example you must set a reference to the Outlook Object Library.

Create a Task

Sub CreateTask()

    Dim olApp As Outlook.Application
    Dim olTsk As TaskItem

    Set olApp = New Outlook.Application
    Set olTsk = olApp.CreateItem(olTaskItem)

    With olTsk
        .Subject = "Update Web Site"
        .Status = olTaskInProgress
        .Importance = olImportanceHigh
        .DueDate = DateValue("06/26/03")
        .TotalWork = 40
        .ActualWork = 20
        .Save
    End With

    Set olTsk = Nothing
    Set olApp = Nothing

End Sub

Comments:

The CreateItem method is used to create the new task.  This method can be used to create just about anything in Outlook; Emails, Contacts, Journal Entries, etc.  olTaskItem is a built in constant that tells VBA which type of item to create.

The With/End With block shows some typical properties that can be set for a TaskItem object.

Both the .Status and .Importance properties are set with built in constants.  You can refer to help to see the other constants available and their corresponding Long Integer values.  Remember that if you use late binding, you must use the intrinsic value, not the built in constant.

The .DueDate property is set using the DateValue function which takes a string argument.  You could also use this syntax to set the .DueDate:

.DueDate = #6/26/2003#

The .TotalWork and .ActualWork properties accept Long Integer data types to set the minutes.  The default time interval in Outlook is hours for these fields, so be aware that in VBA you are setting minutes.  There is also a .PercentComplete property that appears to be totally unrelated to the .TotalWork and .ActualWork properties.  It seems that these properties should compliment each other, but they don't.

Don't forget to .Save, or at least .Display, before your procedure completes.

Prev Page: Contacts        Top of This Page

 

Retrieving a Task

Sub GetTask()

    Dim olApp As Outlook.Application
    Dim olNs As NameSpace
    Dim Fldr As MAPIFolder
    Dim olTsk As TaskItem
    Dim i As Long

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderTasks)
    i = 1

    For Each olTsk In Fldr.Items
        If olTsk.DueDate = #6/26/2003# Then
            ActiveSheet.Cells(i, 1).Value = olTsk.Subject
            i = i + 1
        End If
    Next olTsk

    Set olTsk = Nothing
    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing

End Sub

Comments:

The NameSpace object isn't a typical object.  There is no physical representation of the NameSpace as there is for a Folder or a TaskItem.  It serves as a gateway for access to much of the objects in Outlook's object model.  To get at the NameSpace, you use the GetNameSpace method of the Application object.  The only argument that is supported for this method is "MAPI", so that makes it nice and easy.

The Folder object represents actual folders in Outlook.  To see these folders, choose Folder List from the View menu in Outlook.  You may have nested folders, that is, folders that contain MailItems that are subordinate to the Inbox or folders that contain AppointmentItems that are subordinate to the Calendar.  This procedures uses a default folder which is particularly easy because Outlook gives us the GetDefaultFolder method of the NameSpace object.  If the GetDefaultFolder method wasn't available, that line of code would look like this:

Set Fldr = olNs.Folders("Personal Folders").Folders("Tasks")

Understanding this technique is useful when you need to access a folder that is not a default folder.  Nesting the .Folders property allows access to any folder in Outlook.  You just have to remember that the top folder is Personal Folders and the rest is easy when you see the folder list by using View - Folder List in Outlook.

The meat of the procedure simply uses a For Each statement to loop through all of the TaskItems in the Folder.  When it finds one that satisfies the criteria, it lists the .Subject property on the ActiveSheet.  This example tests for .DueDate of June 26, 2003.  You can test for whatever properties that suit your situation.  Also, once the object variable, olTsk, points to the correct TaskItem, you can do anything you want to it.  If you change anything, be sure to use the .Save method or you will be disappointed.

Prev Page: Contacts        Top of This Page

 

 Home                

 

                    Contact Me            Visit Daily Dose of Excel - an Excel weblog