Convert An Outlook Message Into A Task

UPDATED 7/11/2007: Added handling for email subject line or first line of body to be the task subject; also added Outlook security information to get this to work.

I'm not lucky enough to have a Blackberry so I don't have access to my task list via a mobile device.  I don't generally carry my Pocket PC around and I don't try to get the wonky synchronization to work on my crappy Motorola phone.  When I think of something and I'm out, I use the phone's email capabilities to mail myself a one-liner that has the task information in it, then when I come back to the office I use those as tasks.

To that end, I wanted to set up a rule that would just take those one-liners and process the mail into a task automatically.  Since I use Outlook, I can do that with a rule that runs a script.  Here's how to do this in Outlook 2003.  Not sure, but it might also work in 2007.  Haven't tried.

First, go to "Tools -> Macro -> Visual Basic Editor." This gets you to the script editor.  In the "Project Explorer" open up "Project1" and you should see a folder called "Microsoft Office Outlook."  Open that up and you'll see "ThisOutlookSession."  Double-click that to open it.

You should be looking at a VBA editor window.  If you've already got other scripts going, it might not be empty.  In that window, paste this:

Sub ProcessMailItemIntoTask(Item As Outlook.MailItem)
    Dim strTaskName As String
    strTaskName = Trim(Item.Subject)
    
    If Len(strTaskName) < 1 Then
        ' No subject - use the first line of the body
        strTaskName = Trim(Item.Body)
        Dim intCrLfPos As Integer
        intCrLfPos = InStr(1, strTaskName, Constants.vbCrLf, vbTextCompare)
        If intCrLfPos > 0 Then
            strTaskName = Trim(Left(strTaskName, intCrLfPos - 1))
        End If
    End If
    
    ' Trim TASK: off the line
    Dim intKeyWordPos As Integer
    intKeyWordPos = InStr(1, strTaskName, "TASK:", vbTextCompare)
    If intKeyWordPos = 1 Then
        strTaskName = Trim(Right(strTaskName, Len(strTaskName) - 5))
    End If
    
    ' Create the task
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    objTask.Subject = strTaskName
    objTask.StartDate = Item.ReceivedTime
    objTask.Save
    Set objTask = Nothing
End Sub

What that script does is process any message into a task. It uses the subject line of the mail (or the first line of the body if there is no subject) as the task subject and the time the mail was received as the start time for the task.  You'll notice it also pulls off the word "TASK:" at the beginning of the line - this is going to be a keyword for our rule.

Now that you've got the script, save it and close the VB editor.  Now, back in Outlook, go to "Tools -> Rules and Alerts..."

Select the "New Rule..." option and start from a blank rule.  Select "Check messages when they arrive" and click Next.  Select the conditions "with specific words in the subject or body" and "sent only to me."  For the "specific words" in the subject, use "TASK:" - this will be the magic flag that tells us that this needs to be turned into a task.  For the actions you want to take on the message, select "stop processing more rules," "run a script," "mark it as read," and "delete it."  This will handle the auto-processing feature of the message so it doesn't hang around in your inbox after it's been taskified.  Finally, for the script you want to run, select "Project1.ThisOutlookSession.ProcessMailItemIntoTask."  That will call your script to create a task item out of the subject line of the message.

Once you hit Finish, you're done.  Now when you mail yourself a message with the subject line like "TASK: Take shirts to cleaners" you'll get a task added to your task list with the subject "Take shirts to cleaners."  Pretty simple.  The only drawback is that it's a client-side rule so you have to have Outlook running to process the rule.  That's pretty simple, though - even if you don't have Outlook running all the time, when the client starts up it runs rules and will process all of the appropriate emails next time you fire it up.

Note that if this script tries to read the body of the message, you'll get that annoying "Something is trying to read email addresses in Outlook - allow for X minutes" dialog popping up.  Not sure why it thinks we're looking at addresses if we're only looking at the body, but oh well.

If you have trouble getting this to work, check two things:

First, make sure you've restarted Outlook once before trying it.  For some reason Outlook needs to shut down and restart to take macro changes into effect.

Second, you may need to change your macro security level.  Go to "Tools -> Macro -> Security..." to see your security level.  This will only run in "Medium" or "Low" setting because it's not signed.  If you change your security level, you do so at your own peril.  I'm not responsible if you get hit by the next big Internet worm.  If you don't want to change your security level, you can digitally sign your macro project.  The digital signature route is the recommended way to go to stay safe, but it's the biggest pain, too.

Print | posted @ Tuesday, July 10, 2007 12:28 PM

Comments on this entry:

Gravatar # re: Convert An Outlook Message Into A Task
by madigan at 9/18/2007 8:30 AM

Can you do the same thing but have Outlook close the task for you? I have several hundred monitoring emails and I want a quick way in outlook to see what is up and what is down.
Gravatar # re: Convert An Outlook Message Into A Task
by Travis Illig at 9/18/2007 8:37 AM

In the block where you create the task you could set the task Status to Complete before you save the task. Check out the Status property and the OlTaskStatus enumeration for more information.
Gravatar # re: Convert An Outlook Message Into A Task
by reboyer at 9/18/2007 10:56 AM

Can you have Outlook process based on flag status instead of subject line text?
Gravatar # re: Convert An Outlook Message Into A Task
by Travis Illig at 9/18/2007 11:24 AM

I'm sure you could. Instead of looking at Subject, you could look at the MailItem FlagRequest property.
Gravatar # re: Convert An Outlook Message Into A Task
by Travis Illig at 9/18/2007 11:26 AM

Note that if you do change it to look at a property that you set after the message has already arrived, you'd have to manually re-run the script against the message to convert it. I'd recommend sticking to properties that come in natively on the message, like importance, subject/body, sender, etc. just to make it easier on yourself.
Gravatar # re: Convert An Outlook Message Into A Task
by Louis K at 9/18/2007 3:56 PM

Nice Job! I just tried it in office 2007 and it works great.
Gravatar # re: Convert An Outlook Message Into A Task
by Stephen at 10/2/2007 11:46 PM

Do you know how I would change the code so that the body text of the email plus any attachments would be placed into the text/notes area of the task?
Gravatar # re: Convert An Outlook Message Into A Task
by Travis Illig at 10/3/2007 5:16 AM

Not off the top of my head, no. I'm not really an Outlook/VBA programmer. I coded this thing up to get some simple email-to-task generation going; I leave it as an exercise for the reader to expand on the functionality.
Gravatar # re: Convert An Outlook Message Into A Task
by grmbl at 2/19/2008 7:31 AM

Good job!!! I have an HTC smartphone and whenever i clik/flag an email it comes in task pane but it's not an actual task therefor not showing on my HTC. With this trick I can see my tasks properly now on my phone! Thx a lot. I've also added the feature to add "high priority" to it. See code below:

Select Case MsgBox("High priority?", vbYesNoCancel, "Make task...")
Case vbYes
blnHigh = True
Case vbNo
blnHigh = False
Case vbCancel
Exit Sub
End Select
...
' Create the task
Dim objTask As Outlook.TaskItem
Set objTask = Application.CreateItem(olTaskItem)
'objTask.UserProperties("Activity Priority").Value = "High"
objTask.Subject = strTaskName
objTask.StartDate = Item.ReceivedTime
objTask.ContactNames = Item.SenderEmailAddress
objTask.Body = Item.Body
' Ask for high importance
If blnHigh = True Then
objTask.Importance = olImportanceHigh
End If
objTask.Save
Set objTask = Nothing

BTW: it works fine in OL2007 =)
Gravatar # re: Convert An Outlook Message Into A Task
by Ahsan at 4/24/2008 2:40 PM

This was awesome -- thanks! I'm using it in combination with Jott (I edited the code a little bit) so I can call Jott and automatically have tasks added to my Outlook task list.
Gravatar # re: Convert An Outlook Message Into A Task
by James at 7/30/2008 9:27 PM

While I think this is great, why don't you just left click on the message you want to add, and drag it to the "tasks" menu item? Try it out. When you release the mouse, it will open a new task with the subject line as the task name and the entire email (including headers) will be in the body.

James
Gravatar # re: Convert An Outlook Message Into A Task
by Travis Illig at 7/31/2008 7:54 AM

I didn't do that because I don't want it to be a manual process. I want it to just happen without me having to touch it.
Gravatar # re: Convert An Outlook Message Into A Task
by Charlotte at 8/25/2008 1:26 PM

This sounds cool - never used VB. Can a similar thing be developed to print attachments in e-mails? Or save attachments? (Or is this already in Outlook 2003 somewhere in rules, or something)?
Gravatar # re: Convert An Outlook Message Into A Task
by Travis Illig at 8/25/2008 1:48 PM

I'm sure you probably could. Macros in Outlook can be very powerful. Unfortunately, it really is programming - not something I can outline in one blog post. I recommend you check out the "Microsoft Office Development" section of MSDN for more info: http://msdn.microsoft.com/en-us/library/bb726434.aspx

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 1 and 4 and type the answer here: