VBA – Create an Outlook Task

Below is a simple function which permits you to use VBA to add a task into Outlook using late binding, so no references are required!

'---------------------------------------------------------------------------------------
' Procedure : AddOlTask
' Author    : CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Create a task in Outlook using late binding (no need for references)
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sSubject          : Task subject
' sBody             : Task body (the actual content message in the task)
' dtDueDate         : Task due date
' dtReminderDate    : Task reminder date/time
'
' Usage Example:
' ~~~~~~~~~~~~~~~~
' ? AddOlTask("Meeting with Daniel","Meeting w/ Daniel to discuss the database.", _
'             #5/31/2010#, #5/29/2010 9:00 AM#)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2010-May-20                 Initial Release
'---------------------------------------------------------------------------------------
Function AddOlTask(sSubject As String, sBody As String, _
                    dtDueDate As Date, _
                    dtReminderDate As Date)
On Error GoTo Error_Handler

    Const olTaskItem = 3
    Dim OlApp As Object
    Dim OlTask As Object
    
    Set OlApp = CreateObject("Outlook.Application")
    Set OlTask = OlApp.CreateItem(olTaskItem)
    
    With OlTask
        .Subject = sSubject
        .DueDate = dtDueDate
        .Status = 1                 '0=not started, 1=in progress, 2=complete, 3=waiting,
                                    '4=deferred
        .Importance = 1             '0=low, 1=normal, 2=high
        .ReminderSet = True
        .ReminderTime = dtReminderDate
        .Categories = "Business" 'use any of the predefined Categorys or create your own
        .Body = sBody
        .Save   'use .Display if you wish the user to see the task form and make
                'them perform the save
    End With

Error_Handler_Exit:
    On Error Resume Next
    Set OlTask = Nothing
    Set OlApp = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & "Error Number: " & _
    Err.Number & vbCrLf & "Error Source: AddOlkTask" & vbCrLf & "Error Description: " & _
    Err.Description, vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Update

Things have changed since the article was first published. If you are experiencing issues with the CreateObject(… line, then see my post entitled: CreateObject(“Outlook.Application”) Does Not Work, Now What?

7 responses on “VBA – Create an Outlook Task

  1. Tara

    Thank you for this. I input your code into vba but nothing is showing in my outlook tasks. Which cells in excel need to contain the information. I don’t see a cell reference in your code.

    Thanks.

    1. admin Post author

      I doesn’t reference any cells. It is a stand alone function that expects 4 input variable which it uses to create the task with.

      The usage example celarly illustrates the principle:
      AddOlTask “Meeting with Daniel”, “Meeting w/ Daniel to discuss the database.”, #5/31/2010#, #5/29/2010 9:00 AM#

      So you need to pass the function:
      sSubject : Task subject
      sBody : Task body (the ctual content message in the task)
      dtDueDate : Task due date
      dtReminderDate : Task reminder date/time

      So you can reference cell if you so choose as the input variable.

        1. Daniel Pineault Post author

          You’re going to have to explain your question/comment more because the function enumerates what each input variable is and then even gives a example of how it is used so you see exactly what each variable should be like. So I’m afraid I do not understand what you are requesting. If you explain yourself further, tell me what isn’t clear, I will try to make corrections.