Friday, April 23, 2010

Outlook Email to Task Macro

For several months I have been using ClearContext to categorize and sort my Outlook inbox. One of the features that I liked most about CC is it's abilities to easily create appointments or tasks from an email. However, once the 90 day free trial of ClearContext Pro expires, these utilities are disabled.

Many people probably use the Follow Up flag on the email and call it a day. In fact, I was completely happy with this until just recently. The shortfall with using "follow up" is that it does not create a "task". Both "follow ups" and "tasks" appear in your to-do list in Outlook, and both "follow ups" and "tasks" appear in your tasks pane in Outlook. So, if you don't ever want to do anything but view your tasks in Outlook, you're fine using the follow up flag. However, if you (like me) want to be able to export your outlook tasks in an effort to do something awesome with them, your "follow ups" will be left behind when exporting tasks. (Yes, hopefully that link is a tease to a future post).

I set out to make a macro that:

  1. Made a task out of the currently selected email; and
  2. Used the categories I'd assigned to that email using rules to fill in data for the new task for sorting purposes.
Here is my Macro
Public Sub CreateTaskFromItem()

'This version by Bill Blewett (billblewett.com)
'Modified from KC Lemson's script (http://blogs.technet.com/kclemson/archive/2004/01/31/65586.aspx)
'Which was modified from Jim Durant's script (http://blogs.msdn.com/johnrdurant/archive/2004/01/30/65039.aspx)

Dim olTask As Outlook.TaskItem
'Using object rather than MailItem, so that it
'can handle posts, meeting requests, etc as well
Dim olItem As Object
Dim olExp As Outlook.Explorer
Dim fldCurrent As Outlook.MAPIFolder
Dim olApp As Outlook.Application

Set olApp = Outlook.CreateObject("Outlook.Application")
Set olTask = olApp.CreateItem(olTaskItem)
Set olExp = olApp.ActiveExplorer
Set fldCurrent = olExp.CurrentFolder

Dim cntSelection As Integer
cntSelection = olExp.Selection.Count

For I = 1 To cntSelection
Set olItem = olExp.Selection.Item(I)

'add email as an attachment to the new task
olTask.Attachments.Add olItem

'set subject of the new task
' olTask.Subject = "Follow up on " & olItem.Subject
' I moved this down lower to make the new name ONLY the category that's left over not both original categories

'copy body of email into body of the new task // added by me. I prefer the body text plus the attachment.
olTask.Body = olItem.Body


' =~`~= BEGIN PERSONAL CATEGORY RELATED MODS =~`~=

'==STEP ONE==
'Copy ALL the categories from the Item (e-mail) to the new Task... Simple.

olTask.Categories = olItem.Categories

'==STEP TWO==
'Give the new task the proper overall value using BillingInformation field

If InStr(1, olItem.Categories, "Mark", vbTextCompare) <> 0 Then
olTask.BillingInformation = "Mark"
End If
If InStr(1, olItem.Categories, "Clint", vbTextCompare) <> 0 Then
olTask.BillingInformation = "Clint"
End If
If InStr(1, olItem.Categories, "[2.Clint]", vbTextCompare) <> 0 Then
olTask.BillingInformation = "Clint"
End If
If InStr(1, olItem.Categories, "Me", vbTextCompare) <> 0 Then
olTask.BillingInformation = "Me"
End If

'==STEP THREE==
'Strip the task of ONLY the specific categories which were used above to create BillingInformation values.

olTask.Categories = Replace(olTask.Categories, "Mark", "", , vbTextCompare)
olTask.Categories = Replace(olTask.Categories, "Clint", "", , vbTextCompare)
olTask.Categories = Replace(olTask.Categories, "Me", "", , vbTextCompare)
olTask.Categories = Replace(olTask.Categories, "[2 ]", "", , vbTextCompare)
olTask.Categories = Replace(olTask.Categories, "[2. ]", "", , vbTextCompare)
olTask.Categories = Replace(olTask.Categories, "[2]", "", , vbTextCompare)


' =~`~= END PERSONAL CATEGORY RELATED MODS =~`~=


olTask.Subject = olTask.Categories & " - Follow Up, " & olItem.Subject 'moved from above to include the proper category

' Misc Cleanups for task names and categories

'to clean up the new task's subject use:
'olTask.Subject = Replace(olTask.Subject, "Text to be replaced", "Text to replace it with", , vbTextCompare)
'
'to clean up the new task's categories use:
'olTask.Categories = Replace(olTask.Categories, "Text to be replaced", "Text to replace it with", , vbTextCompare)

olTask.Subject = Replace(olTask.Subject, "[1.", "", , vbTextCompare)
olTask.Subject = Replace(olTask.Subject, "]", "", , vbTextCompare)
olTask.Subject = Replace(olTask.Subject, "[6 ", "", , vbTextCompare)

Next

'Make the new task display - delete or comment it out (') to not show
olTask.Display

'Set the due date for today
olTask.DueDate = Date

'No Reminder, but if it was True it would remind in 3 hours
'the original author of this script had the reminder as True
'and used the 3 hour mark to make sure that she had all of the
'settings correct... it's super annoying to me.
olTask.ReminderSet = False
olTask.ReminderTime = DateAdd("h", 3, Now)

'Saving the new task automatically
olTask.Save

End Sub
Now let's take a look at what this is doing.

The first third of the macro is creating a new task, pasting the text of the email into the task and also attaching the email to the task.
The second third of the macro is my custom categorizing. If an email has a category of "Me" "Mark" or "Clint" then that value is given to the "BillingInformation" field of the task. The purpose of this is so that when viewing my tasks I can have them sorted by categories for each client, but also have a higher value in the hierarchy for whose client they are. I then wanted to strip the task of the "BillingInformation" category since I am already using this information somewhere else, and having multiple categories in a task would lead to duplicate entries in my custom task view. However, I wanted to keep the client specific category in tact. This was trickier than I expected.
The final third of the macro cleans up some left over oddities from category names, sets the due date and reminder value of the new task, then opens and saves the new task.

This macro works perfect for what I was trying to do. You will need to customize to suit your specific needs, but I've left comments in the code that should help with that.

If you need help installing the macro see this article.

2 comments:

  1. Some days ago I saw my friend and he told me about his problem with corrupted emails from his young sister. I had been thinking of this problem for several days when I found out - repair outlook pst file. To my great surprise it aided him and my friend presented me a nice gift:)

    ReplyDelete
  2. Very nice macro, works as expected! Thank you very much! Tamás

    ReplyDelete