Outlook Item to Task Macro


The following macros convert outlook items to tasks with different categories. Based on this blog post which in turn is based on this one.

  • The item body is copied to the task body and the item itself is added as an attachment.
  • If multiple items are selected, multiple tasks will be created.
  • Created tasks are left open for editing.
  • I am in two minds about deleting the item after the task is created and have decided against it at the moment.
  • It appears that OL 2007 does not allow shortcut keys to be assigned to macros .

Public Sub CreateInboxTaskFromItem()
    CreateCatagorisedTaskFromItem (“2 inbox”)
End Sub

Public Sub CreateWaitingTaskFromItem()
    CreateCatagorisedTaskFromItem (“2 waiting for”)
End Sub

Public Sub CreateSomedayTaskFromItem()
    CreateCatagorisedTaskFromItem (“2 someday maybe”)
End Sub

Public Sub CreateTaskFromItem()
    CreateCatagorisedTaskFromItem (“”)
End Sub

Private Sub CreateCatagorisedTaskFromItem(catagory As String)

  Dim olTask As Outlook.TaskItem
  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 olExp = olApp.ActiveExplorer
  Set fldCurrent = olExp.CurrentFolder
 
  Dim cntSelection As Integer
  cntSelection = olExp.Selection.Count
 
  For i = 1 To cntSelection
    Set olTask = olApp.CreateItem(olTaskItem)
    Set olItem = olExp.Selection.Item(i)
    olTask.Attachments.Add olItem
    olTask.Body = olTask.Body + olItem.Body
    If catagory = “2 waiting for” Then
        olTask.Subject = olItem.SenderName & “: ” & olItem.Subject
    Else
        olTask.Subject = olItem.Subject
    End If
    olTask.Categories = catagory
    ‘olItem.Delete
    olTask.Display
    olTask.Save
  Next
 
End Sub

  1. #1 by Sandra on July 20, 2007 - 15:20

    Just curious – how would you specify a form to use when creating the task? I have tried the original version of this from Lemson, but even thought my task default form (customized) is set correctly in Outlook, the new task doesn’t use it.

    Thanks.

  2. #2 by Sandra on July 20, 2007 - 15:21

    I have tried this macro from Lemson. Question: how would I specify a custom form to be used in tasks? I have the default form in tasks set correctly in Outlook, but the Macro doesn’t use it.

    Thanks.

  3. #3 by John Plummer on July 21, 2007 - 19:31

    Hi Sandra,
    I do not use custom forms in Outlook but the best outlook resource on the web is probably http://www.outlookcode.com/. There is a good community there and someone should be able to help you.

  4. #4 by Charles on August 10, 2007 - 05:20

    Great Macro! How can I have the Macro create a task from the message I have open instead of what is selected in the table view?

  5. #5 by John Plummer on August 11, 2007 - 12:27

    Hi Charles,
    This is untested but I would guess that the following function should do it:

    Private Sub CreateCatagorisedTaskFromOpenItem(catagory As String)

    Dim olTask As Outlook.TaskItem
    Dim olItem As Object
    Dim olApp As Outlook.Application
    Dim olIns As Outlook.Inspector

    Set olApp = Outlook.CreateObject(”Outlook.Application”)
    Set olIns = olApp.ActiveInspector

    Set olTask = olApp.CreateItem(olTaskItem)
    Set olItem = olIns.CurrentItem
    olTask.Attachments.Add olItem
    olTask.Body = olTask.Body + olItem.Body
    If catagory = “2 waiting for” Then
    olTask.Subject = olItem.SenderName & “: ” & olItem.Subject
    Else
    olTask.Subject = olItem.Subject
    End If
    olTask.Categories = catagory
    ‘olItem.Delete
    olTask.Display
    olTask.Save

    End Sub

    Then Change the calling functions to call CreateCatagorisedTaskFromOpenItem rather than CreateCatagorisedTaskFromItem.

  6. #6 by Yasmeen on December 5, 2007 - 23:56

    John,

    Excellent macro, I’ve been looking for something like this.

    Just some feedback: your code causes Outlook 2003 to prompt with the “Allow access to” pop-up, which is annoying.

    To prevent that from happening, you need to change:

    Set olApp = Outlook.CreateObject(”Outlook.Application”)

    to:

    Set olApp = Application

    The CreateObject caused that issue.

    Cheers,

  7. #7 by Dwayne on August 21, 2008 - 03:27

    John I copied your code into outlook2007. Each time I run one of the macros I get a syntax error on either line around 2 – 13 depending on the macro. Any suggestions? Thanks.

  8. #8 by John Plummer on August 21, 2008 - 22:31

    Hi Dwayne,
    I would check that copying and pasting from html has not introduced and character substitutions, especially the double quotes.
    It may be worth copying and pasting into notepad then copying and pasting from there as a text editor as an intermediate stage can often weed out things like smart quotes.

    If that doesn’t work it may be worth posting the question on http://www.outlookcode.com/

  9. #9 by Andrew on January 9, 2009 - 14:35

    I’m trying to create an If statement or some line of code that would allow me to keep the email if I exit out of the macro without saving, but would delete the item if I saved and closed it as a task. Any ideas?

  10. #10 by John Plummer on January 9, 2009 - 17:36

    Andrew, I guess that would be possible but I doubt it would be simple. You would be better off asking the question on http://www.outlookcode.com/.

  11. #11 by Smaran on January 14, 2009 - 22:20

    I run your code. it is not giving any errors. But I cant see the tesks created in the Outlook task list.

  12. #12 by John Plummer on January 21, 2009 - 11:11

    Smaran,
    It should bring up the task form. Are you sure it is running? You could try setting a breakpoint in the macro and see if that gets hit.

    I have had something similar happen in the past and a reboot solved it so may be worth trying that first.

  13. #13 by antonisx4 on May 14, 2009 - 09:33

    I have copied the code for Outlook Item to Task Macro but it’s giving me an error when I am trying to compile it and of course is not working. what can I do to fix it?

  14. #14 by Priit on June 2, 2009 - 19:37

    to Dwayne and John
    you have to rewrite quotation marks and apostrophes – then it works without errors.

  15. #15 by Simon on November 10, 2009 - 16:38

    Hi Charles,

    I cannot run the macro OL 2003.

    Is it possible to used a adopted macro in the 2003 OL version as well?

    Thanks,

  16. #16 by Bernardo on February 25, 2010 - 19:26

    Hello, thanks for posting this.
    The code works fine for me. However, I would like to include also the header of the message (from, sent,subject), as it would be included if I dragged and dropped on the task button. Do you have any ideas on how to do it?

    Thanks!

  17. #17 by John Plummer on February 25, 2010 - 20:00

    The From and Subject are already in the code. I imagine something along the lines of:
    olTask.Body = olTask.Body + “From: ” + olItem.SenderName + vbNewLine
    olTask.Body = olTask.Body + “Sent: ” + olItem.SentOn + vbNewLine
    olTask.Body = olTask.Body + “Subject: ” + olItem.Subject + vbNewLine + vbNewLine
    olTask.Body = olTask.Body + olItem.Body

    should work. (not tried it)

  18. #18 by Ronaque on June 2, 2010 - 01:42

    Hello,

    Can u plz tell me where this code needs to be pasted and in which format it should be (either .bat or .vbs)……..

    Many Thnx…..

  19. #19 by Ronaque on June 2, 2010 - 02:49

    Hey i pasted the code in OL macro session but its not running as it should b.

    can you plz forward the complete steps n procedure of the same on my mail ID:

    Husain@systems-24.com.

    Thnx in Advance
    Ronn

  20. #20 by John Plummer on June 2, 2010 - 11:04

    Hi Ronaque,
    I am not using Outlook atm so can’t walk you through it, but you should be able to paste the code into the macro editor. If you are unsure, check the Outlook help files. One thing to watch for is sometimes single and double quotes get changed for different characters.

(will not be published)