Create a Task when a message is flagged

Last reviewed on December 30, 2013

You can use the ItemChange event to watch for any change to an item. In this example, the code watches for the QuickFlag block to be clicked then creates a task for the message.

In addition, the flag is cleared and a category applied to the message, so you know that a task exists for the item. Remove those lines if you want the message flag to remain.

This macro needs to go into ThisOutlooksession. To test the macro, click in Initialize_handler then click the Run button. Flag a message and the macro should create a task, displaying it onscreen.

See How to use the VBA Editor if you don't know how to use macros.

Public WithEvents OlItems As Outlook.Items

Public Sub Initialize_handler()
   Set OlItems = Application.GetNamespace("MAPI"). _
        GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub OlItems_ItemChange(ByVal Item As Object)
      
If Item.IsMarkedAsTask = True Then

Dim Ns As Outlook.NameSpace
Dim olTask As Outlook.TaskItem

Set Ns = Application.GetNamespace("MAPI")

Set olTask = Ns.GetDefaultFolder(olFolderTasks) _
    .Items.Add(olTaskItem)

With olTask
        .Subject = Item.Subject
        .Attachments.Add Item
        .Body = Item.Body
        .DueDate = Now + 1
        .Save
        .Display
End With

With Item
 .ClearTaskFlag
 .Categories = "Task"
 .Save
End With 

Set Ns = Nothing

End If


End Sub

Set Categories based on the chosen flag

This code sample uses the flags (today, tomorrow, this week, next week) to set the Task due date and determine the category assigned to the email.

This code subtracts today's date from the due date then matches the result with a category. "No date" actually uses a date in year 4501 and in my sample code, I'm looking for differences greater than 20 days.

Public WithEvents OlItems As Outlook.Items

Public Sub Initialize_handler()
    Set OlItems = Application.ActiveExplorer.CurrentFolder.Items
End Sub

Private Sub OlItems_ItemChange(ByVal Item As Object)

If Item.IsMarkedAsTask = True Then
     Dim Ns As Outlook.NameSpace
     Dim olTask As Outlook.TaskItem
     Dim newAttachment As Outlook.Attachment
     Dim aDate  As Date
    Dim cat As String
    
    Set Ns = Application.GetNamespace("MAPI")
    Set olTask = Ns.GetDefaultFolder(olFolderTasks).Items.Add(olTaskItem)
    Set newAttachment = olTask.Attachments.Add(Item, Outlook.OlAttachmentType.olEmbeddeditem)
    aDate = Format(Now, "mm/dd/yyyy")

    With olTask
        .Subject = Item.Subject
        .DueDate = Item.TaskDueDate
        .Save
        .Display
    End With
   

Select Case Item.TaskDueDate - aDate

    Case 0
         cat = ".Today"
    
    Case 1, 2
        cat = ".SOON"
    
    Case 3, 4, 5, 6, 7
        cat = ".WEEK"
    
    Case Is <= 20
        cat = ".FUTURE"
    
    Case Is > 20
       cat = ".NO DATE"
        
End Select
    
With Item
    .ClearTaskFlag
    .Categories = cat
    .Save
End With

Set Ns = Nothing
End If

End Sub

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

If the Post Coment button disappears, press your Tab key.