Class: OutlookTasks in Category Microsoft Outlook : Automation from Total Visual SourceBook

Working with Microsoft Outlook Tasks through Automation using VBA and VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the COutlookTasks class.
AppNameSpace Property Get a handle to the current instance of the Outlook NameSpace.
AppOutlook Property Get a handle to the current instance of Outlook.
Delimiter Property Set the value of the Delimiter property to modify how muliple recipient and attachment values are defined.
Class_Initialize Initialize Initialize default delimiter.
AddTask Method Add a user Task by providing the basic options.
AddTaskRequest Method Add and send a Task Request to one or more recipients.
DeleteTasks Method Delete a Personal Task Item.
GetTaskDueDate Method Get the Due Date of a Task Item.
GetTaskItem Method Get the first task item that satisfies the specified criteria. With the item, you can edit its properties directly.
GetTaskList Method Get a list of all Task Items.
GetTaskListByDueDate Method Get a list of all Task items due within a date range.
GetTaskFilter Private Create the filter string to limit the task items.
StartOutlook Method Starts an instance of Outlook.
Class_Terminate Terminate Clean up class.
CloseOutlook Method Close an instance of Outlook.
' Example of the COutlookTasks class
'
' To use this example:
' 1.  Create a new form
' 2.  Create the following command buttons:
'        - cmdGetTaskList
'        - cmdAddTask
'        - cmdGetTaskDueDate
'        - cmdSendTask
'        - cmdDeleteTask
' 3.  Create the following textboxes
'        - txtSubject
'        - txtDateDue
'        - txtDateStart
'        - txtDateReminder
'        - txtTo
'        - txtAttachment
'        - txtBody
' 4. Run the form

Private Sub cmdAddTask_Click()
  Dim clsOutlookTasks As COutlookTasks
  Dim fOK As Boolean

  If Nz(Me.txtDateDue) = "" Then
    MsgBox "Please enter a due date."
    Me.txtDateDue.SetFocus
  Else
    If MsgBox("Are you sure you want to add personal task " & vbCrLf & "   " & Me.txtSubject & vbCrLf & "Due on " & Me.txtDateDue & "?", vbYesNo) = vbYes Then

      ' Initialize Tasks class and connect to Outlook
      Set clsOutlookTasks = New COutlookTasks
      clsOutlookTasks.StartOutlook

      fOK = clsOutlookTasks.AddTask(Nz(Me.txtSubject), Me.txtDateDue, Nz(Me.txtDateStart), Nz(Me.txtDateReminder), Nz(Me.txtBody), Nz(Me.txtAttachment), True)

      Set clsOutlookTasks = Nothing

      MsgBox "Task added successfully: " & fOK

    End If
  End If

End Sub

Private Sub cmdDeleteTask_Click()
  Dim strSubject As String
  Dim clsOutlookTasks As COutlookTasks
  Dim lngDeleted As Long

  strSubject = InputBox("Enter the subject to delete", , Me.txtSubject)

  If strSubject <> "" Then

    If MsgBox("Are you sure you want to delete all personal tasks with the subject:" & vbCrLf & "'" & strSubject & "'" & "?", vbYesNo) = vbYes Then

      ' Initialize Tasks class and connect to Outlook
      Set clsOutlookTasks = New COutlookTasks
      clsOutlookTasks.StartOutlook

      lngDeleted = clsOutlookTasks.DeleteTasks(strSubject)

      ' Clean up
      Set clsOutlookTasks = Nothing

      MsgBox lngDeleted & " tasks deleted"

    End If
  End If

End Sub

Private Sub cmdEditTask_Click()
  ' Retrieve and edit a task by increasing the due date by one day

  Dim strSubject As String
  Dim clsOutlookTasks As COutlookTasks
  Dim outTask As Outlook.TaskItem

  strSubject = InputBox("Enter the subject to search add a day to its due date", , Me.txtSubject)

  If strSubject <> "" Then

    ' Initialize Tasks class and connect to Outlook
    Set clsOutlookTasks = New COutlookTasks
    clsOutlookTasks.StartOutlook

    ' Retrieve and open the first task for the selected subject (in this example, no date filter is provided for this task)
    Set outTask = clsOutlookTasks.GetTaskItem(True, strSubject, 0)

    If outTask Is Nothing Then
      MsgBox "No task was found for:" & vbCrLf & strSubject
    Else
      ' Add one day to the due date
      outTask.DueDate = outTask.DueDate + 1

      ' Save the change
      outTask.Save

      If outTask.Recipients.Count > 0 Then
        If MsgBox("Do you want to send the changes to your Task recipients?", vbYesNo) = vbYes Then
          outTask.Send
        End If
      End If

      MsgBox "The due date for this Task was extended by one day:" & vbCrLf & strSubject
    End If

    Set clsOutlookTasks = Nothing
  End If

End Sub

Private Sub cmdGetTaskDueDate_Click()
  ' Get the due date for a task

  Dim strSubject As String
  Dim clsOutlookTasks As COutlookTasks
  Dim datDue As Date

  strSubject = InputBox("Enter the subject to search add a day to its due date", , Me.txtSubject)

  If strSubject <> "" Then

    ' Initialize Tasks class and connect to Outlook
    Set clsOutlookTasks = New COutlookTasks
    clsOutlookTasks.StartOutlook

    datDue = clsOutlookTasks.GetTaskDueDate(strSubject)

    Set clsOutlookTasks = Nothing

    If datDue = 0 Then
      MsgBox "Task " & strSubject & " was not found"
    Else
      MsgBox "The due date for task " & strSubject & " is: " & vbCrLf & datDue
    End If

  End If

End Sub

Private Sub cmdGetTaskList_Click()
  ' Get list of tasks

  Dim clsOutlookTasks As COutlookTasks
  Dim colTasks As New Collection
  Dim intCounter As Integer
  Dim lngTasks As Long

  ' Initialize Tasks class and connect to Outlook
  Set clsOutlookTasks = New COutlookTasks
  clsOutlookTasks.StartOutlook

  Set colTasks = clsOutlookTasks.GetTaskList

  ' Clean up
  Set clsOutlookTasks = Nothing

  lngTasks = colTasks.Count

  For intCounter = 1 To lngTasks
    With colTasks(intCounter)
      Debug.Print .DueDate, .Subject
    End With
  Next intCounter

  Set colTasks = Nothing

  MsgBox lngTasks & " tasks (listed in the Immediate window)."

End Sub

Private Sub cmdGetTaskListDateRange_Click()
  ' Get list of tasks for a date or date range

  Dim strDue As String
  Dim datDue As Date
  Dim clsOutlookTasks As COutlookTasks
  Dim colTasks As New Collection
  Dim intCounter As Integer
  Dim lngTasks As Long

  strDue = InputBox("Retrieve all the tasks with this due date:", , Date)

  If strDue <> "" Then
    datDue = CDate(strDue)

    ' Initialize Tasks class and connect to Outlook
    Set clsOutlookTasks = New COutlookTasks
    clsOutlookTasks.StartOutlook

    ' Get and open all the tasks due on the selected date
    Set colTasks = clsOutlookTasks.GetTaskListByDueDate(datDue, datDue, True)

    ' This is an example of getting all the tasks due on or before the selected date (without opening them)
    'Set colTasks = clsOutlookTasks.GetTaskListByDueDate(0, datDue, False)

    ' Clean up
    Set clsOutlookTasks = Nothing

    lngTasks = colTasks.Count

    For intCounter = 1 To lngTasks
      With colTasks(intCounter)
        Debug.Print .DueDate, .Subject
      End With
    Next intCounter

    Set colTasks = Nothing

    MsgBox lngTasks & " tasks were found (listed in the Immediate window)."
  End If

End Sub

Private Sub cmdSendTask_Click()
  Dim clsOutlookTasks As COutlookTasks

  If Nz(Me.txtDateDue) = "" Then
    MsgBox "Please enter a due date"
    Me.txtDateDue.SetFocus
  ElseIf Nz(Me.txtTo) = "" Then
    MsgBox "Please enter a recipient to send the task to"
    Me.txtTo.SetFocus
  Else
    If MsgBox("Are you sure you want to send task request " & vbCrLf & "   " & Me.txtSubject & vbCrLf & "To " & Me.txtTo & "?", vbYesNo) = vbYes Then
      ' Initialize Tasks class and connect to Outlook
      Set clsOutlookTasks = New COutlookTasks
      clsOutlookTasks.StartOutlook

      ' Create task request
      Call clsOutlookTasks.AddTaskRequest(Nz(txtTo), Nz(txtSubject), txtDateDue, Nz(txtDateStart), Nz(txtBody), Nz(Me.txtAttachment), True)

      ' Clean up
      Set clsOutlookTasks = Nothing
    End If
  End If

End Sub

Private Sub Form_Load()

  ' Set up the controls

  Const cintCmdWidth As Integer = 3100
  Const cintCmdHeight As Integer = 375
  Const cintCmdLeft As Integer = 5500

  With Me.txtSubject
    .Value = ""
    .Top = 500
    .Left = 500
    .Width = 4500
    .Height = 300
  End With

  With Me.txtDateDue
    .Format = "General Date"
    .Value = Date + 1
    .Top = 900
    .Left = 1500
    .Width = 3000
    .Height = 300
  End With

  With Me.txtDateStart
    .Format = "General Date"
    .Value = Date
    .Top = 1400
    .Left = 1500
    .Width = 3000
    .Height = 300
  End With

  With Me.txtDateReminder
    .Format = "General Date"
    .Value = Date + 1 - (1 / 24)
    .Top = 1900
    .Left = 1500
    .Width = 3000
    .Height = 300
  End With

  With Me.txtTo
    .Value = "NoOne@fmsinc.com"
    .Top = 2300
    .Left = 500
    .Width = 4500
    .Height = 300
  End With

  With Me.txtAttachment
    .Value = "C:\Total Visual SourceBook 2013\Samples\Test.txt"
    .Top = 2700
    .Left = 500
    .Width = 4500
    .Height = 300
  End With

  With Me.txtBody
    .Value = ""
    .Top = 3100
    .Left = 500
    .Width = 4500
    .Height = 5000
  End With

  ' Command buttons

  With Me.cmdGetTaskList
    .Caption = "Get Task List"
    .Top = 500
    .Left = cintCmdLeft
    .Width = cintCmdWidth
    .Height = cintCmdHeight
  End With

  With Me.cmdGetTaskListDateRange
    .Caption = "Get Task List a Date"
    .Top = 1000
    .Left = cintCmdLeft
    .Width = cintCmdWidth
    .Height = cintCmdHeight
  End With

  With Me.cmdAddTask
    .Caption = "Add Task"
    .Top = 1500
    .Left = cintCmdLeft
    .Width = cintCmdWidth
    .Height = cintCmdHeight
  End With

  With Me.cmdGetTaskDueDate
    .Caption = "Get Task Due Date"
    .Top = 2000
    .Left = cintCmdLeft
    .Width = cintCmdWidth
    .Height = cintCmdHeight
  End With

  With Me.cmdEditTask
    .Caption = "Edit Task"
    .Top = 2500
    .Left = cintCmdLeft
    .Width = cintCmdWidth
    .Height = cintCmdHeight
  End With

  With Me.cmdSendTask
    .Caption = "Send Task Request"
    .Top = 3000
    .Left = cintCmdLeft
    .Width = cintCmdWidth
    .Height = cintCmdHeight
  End With

  With Me.cmdDeleteTask
    .Caption = "Delete Task"
    .Top = 3500
    .Left = cintCmdLeft
    .Width = cintCmdWidth
    .Height = cintCmdHeight
  End With

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS