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. A video of the macro in action is at the end of the article.
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
Using the macro
This video shows the first macro in action. You need to have macro security set to low to test it.
Use Categories, not Flags
You can watch for categories to be added to the message. In this example, I'm checking for the most recently added category and if there is a match, forward it to another address.
You can use a simple IF... Then statement to forward if you need to forward only one category; the select case statement lets use use multiple categories to send to different addresses.
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) Dim strForward As String Dim arrForward As Variant Dim arrCat As Variant Dim oForward As MailItem If Item.Categories = "Sent" Then Exit Sub arrCat = Split(Item.Categories, ",") If UBound(arrCat) >= 0 Then strCat = Trim(arrCat(0)) Debug.Print strCat Else Exit Sub End If Select Case strCat Case "Email Admin" strForward = "email@example.com" Case "Operations" strForward = "firstname.lastname@example.org" Case "Billing" strForward = "email@example.com" Case "Accounting" strForward = "firstname.lastname@example.org" Case Else Exit Sub End Select Set oForward = Item.Forward oForward.To = strForward oForward.Display ' for testing 'oForward.send 'to send With Item .Categories = "Forwarded;" & Item.Categories .Save End With End Sub
How to use the macro
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 and up, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.
To use the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
More information as well as screenshots are at How to use the VBA Editor.