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

22 thoughts on “Outlook Item to Task Macro

  1. 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. 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. 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?

  4. 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.

  5. 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,

  6. 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.

  7. 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/

  8. 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?

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

  10. 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.

  11. 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?

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

  13. 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,

  14. 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!

  15. 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)

  16. 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…..

  17. 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

  18. 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.

  19. Great Job!
    Any idea how to set the start and due dates (from the body of the email). Say they are always written the same way
    Subject: New Task!
    Body:
    Hello,

    You have a new task
    Start Date: 2011-01-02
    Due Date: 2011-02-02

    Can that be done?

  20. Hi,

    I ve some code in bas format now i goto press Alt+F11 show vb editor window now i import the bas file but the code not run at send button click
    in this code in bas file..
    Public WithEvents myOlApp As outlook.Application

    Public Sub Initialize_handler()
    Set myOlApp = CreateObject(“Outlook.Application”)
    End Sub

    Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
    End Sub

    i dont need copy and paste code in vb editor window . i wanna how import and run this code let u pls explain this………..

    its urgent

Leave a Reply