Last reviewed on November 22, 2014   —  165 Comments

I regularly convert many of my emails into tasks, is there a way to create a rule that will automatically convert emails with a specific subject line, into a task?

Sure, you can use a simple script that creates a task from an email and use it with a Run a Script rule. We also added a second code sample that creates a task from the message you are reading in the reading pane.

To create an appointment from an email message, see Create an Outlook appointment from an email message.

VBA to Convert an Email to a Task

Press Alt+F11 to open the VBA editor and expand the tree to find ThisOutlookSession. Paste this code into the VBA editor then create a rule and choose Run a script as the action.

Sub ConvertMailtoTask(Item As Outlook.MailItem)
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
With objTask
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Item.Body
    .Save
End With
    Set objTask = Nothing
End Sub

You can customize the script to set a category or due date (or any other fields supported in Tasks). To calculate the due date, you'll use the DueDate property and add a whole number to the Received date. If you prefer to change the Start date, you would add a whole number to the start date line. If you use a category that is not in your Categories list, the color category field in the view will be colorless as the category is not in the master list.


    objTask.StartDate = Item.ReceivedTime + 2
    objTask.DueDate = Item.ReceivedTime + 3
    objTask.Categories = "Slipstick"

To use today's date only, not the current time, use Date and decimals to set the due date or reminder time.

    .DueDate = Date + 3.5 '3 days at noon
    .ReminderTime = Date + 2.25 ' remind at 6 AM 2 days from now

To override your default settings for Task reminders (set in Options, Tasks), you can use the ReminderSet and ReminderTime properties.

This example creates a reminder for 6 hrs before the due date or 6 pm the day before. If a reminder time is not set, it will use the default setting for Tasks, usually 8 am on the due date.

Accepted values for ReminderSet are True (on) or False (no reminder)


    objTask.ReminderSet = True 
    objTask.ReminderTime = objTask.DueDate - 0.25
    

Create a task using a Run a Script rule video tutorial

Working with Attachments

To add the item as an attachment to the task, add the following lines after the Item.Body line:

 Dim newAttachment As Outlook.Attachments

 Set newAttachment = objTask.Attachments
   newAttachment.Add Item, olEmbeddeditem

To copy attachments from the messages to the task, get the CopyAttachments function and add it at the end of the task macro. Then add this code before the .save

If Item.Attachments.Count > 0 Then
CopyAttachments Item, objTask
End If

Send a Task Request

If you want to send a Task Request, you need to Assign it and add the Recipient. You can use either the full email address or the GAL alias, or even the person's display name, provided the name can resolve to an entry in your address book or GAL. I recommend using the address only because it can't resolve to the wrong person.

You'll need to add .Assign to the With objTask code block, along with .Recipients.Add "email@address.com" line. You'll also need to change .Save to .Send

With objTask
    .Assign
    .Recipients.Add "email@address.com"
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Item.Body
    .Send
End With

If you use the user's name or alias, you may need to Resolve it using code before it's sent. .Assign goes in the With objTask code block, but the Recipient is added outside of the With statement, then it's sent.

The DIM statement goes at the top of the macro with the other DIM statements.


 Dim myDelegate As Outlook.Recipient

With objTask
    .Assign
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Item.Body
End With

 Set myDelegate = objTask.Recipients.Add("alias")
 myDelegate.Resolve

objTask.Send

Add Categories to the tasks

Carol asked how to add a category to the tasks. This is easy if you want one category for all tasks - just place this line before the objTask.Save line:

objTask.Categories = "keyword" 

To assign different categories based on different keywords in the subject, you can use an IF statement for a couple of keywords but should use an array for a larger number of keywords. Instructions are at Using Arrays with Outlook macros.

Save to a different tasks folder

You can save the task to a different folder by adding two lines to the code. This example saves it to a Task folder in a different data file, in this case, a Sharepoint task list that is linked to Outlook.

You can also use a different folder in the mailbox. When the folder is the same level as the Tasks folder, using the example below.

Set SPSFolder = Session.GetDefaultFolder(olFolderTasks).Parent.Folders("Task folder Name")

When the folder is a subfolder under the default Tasks folder, use this:

Set SPSFolder = Session.GetDefaultFolder(olFolderTasks).Folders("Subfolder Name")

See Working with non-default Outlook Folders for more information.

    
' You need the GetFolderpath function from 
' http://slipstick.me/qf#GetFolderPath
Set SPSFolder = GetFolderPath("SharePoint Lists\Slipstick - Tasks")
    
    Set objTask = Application.CreateItem(olTaskItem)
    ' do whatever
    ' 
    objTask.Save

' After you save the task, move it to the other folder
objTask.Move SPSFolder
 

Create the rule

Create a rule to run a scriptCreate a rule using the condition and choose the action "Run a Script", choosing the ConvertMailtoTask script you pasted into the VBA editor. Complete the rule.

When a new message arrives meeting the conditions in the rule, the script runs and creates a task out of the message.

Create Task from the selected message

Jason wanted to know if we could use the code with a button to create a task from a selected message.

To do this, we needed to modify the original code just a little. To use it, create a command on the ribbon or toolbar and assign the macro to it. When you are reading a message in the reading pane, you can click the button to create a task.

This code doesn't work with open messages, but a little more tweaking can change that.

Sub ConvertSelectedMailtoTask()
    Dim objTask As Outlook.TaskItem
    Dim objMail As Outlook.MailItem
    
    Set objTask = Application.CreateItem(olTaskItem)
  
    For Each objMail In Application.ActiveExplorer.Selection

With objTask
    .Subject = objMail.Subject
    .StartDate = objMail.ReceivedTime
    .Body = objMail.Body
    .Save
End With
    
    Next
    Set objTask = Nothing
    Set objMail = Nothing
End Sub

Tools

SimplyFile for Microsoft Outlook

SimplyFile helps you file incoming and outgoing messages to the right Outlook folder with one click of a mouse. SimplyFile's state of the art algorithm learns as you file messages and suggests destination folders for filing. All you have to do to send a message to the right folder is click a button. SimplyFile also includes buttons for turning messages into Tasks and Appointments. Compatible with Outlook 2000, 2002, 2003 and 2007. Version 2.3.4.106.

Comments

  1. Mike Hearn says

    Great site with plenty of information thanks a lot.

    I need to set up the address box on outlook 2010 contacts page so that the first line is "home1" and the second line is "home2" third line "home3" etc etc

    I have had a look at the developer and installed and presume that this is the way to set it up.

    Any tips advice or videos to help me please ? Have tried export file but no way can i get the file spilt - though it does recognize the city and post code .

    Many thanks

  2. Jason says

    Would it be possible to assign this event to a button (macro) by customizing the toolbar of the message form? I would like to be able to trigger the event while reviewing the message, as opposed to creating the task based on a rule.

    • Diane Poremsky says

      Of course it's possible. The code needs just a little tweaking -
      Sub ConvertMailtoTask()
      Dim objTask As Outlook.TaskItem
      Dim objMail As Outlook.MailItem

      Set objTask = Application.CreateItem(olTaskItem)
      For Each objMail In Application.ActiveExplorer.Selection

      objTask.Subject = objMail.Subject
      objTask.StartDate = objMail.ReceivedTime
      objTask.Body = objMail.Body
      objTask.Save

      Next
      Set objTask = Nothing
      Set objMail = Nothing
      End Sub

  3. Jesse says

    Hello,
    After a solid 3 hours of searching for solutions to what I thought would be a standard feature, I was amazed that the code you listed above worked!

    The wrinkle: I would like the created task to be created in a shared task folder rather than my default task folder... ie, I have a custom task folder called "Freight" which can be accessed by anyone in our freight department and I want the auto-generated task to be there.

    Thanks for any (more) assistance you could provide...

  4. Ike says

    Thanks for the useful code.

    Is it possible to create a task from an email attachment?

    If I also have multiple attachments in one email is it possible to create multiple tasks i.e. One task is generated for each attached item.

    • Diane Poremsky says

      Adding the attachment to a task is possible, taking content from the attachment and using it to create a task would take a lot more code.

  5. Timo says

    This is very helpful site nad excelent code!

    How can I add a red flag for the task. Or is there any other way that I could get my mailconverted tasks to get shown to me more easier. Now I have to go to tasks and select right task list to see these converted.
    if I would get a flag on it it would come up more!

    I am starting these things so...

    • Diane Poremsky says

      Tasks have flags by default. You might want due date & reminders -
      objTask.DueDate = objMail.ReceivedTime + 3 ' due in 3 days
      objTask.ReminderSet = True
      objTask.ReminderTime = objTask.DueDate - 0.25 ' this is 6 hours before its due

      A filtered view might help - or assigning a category. Create a category and assign a color then add objTask.Categories = "Category-name" above the line that saves it. Filter task list shows how to hide flagged email, but you can use the same method to hide or show tasks meeting other conditions.

  6. Jeff says

    Great code!

    is it possible to add the first line of the body of the email to a user defined field? My email will have the name of the person sending the request, I have a user defined field in my task for Requested by.

    • Diane Poremsky says

      I forget offhand if you can use custom fields, but try this:

      objTask.customfield = left (Item.body,20) 'where 20 is the # of characters you want from the first line

      The results might be goofy if the first line is shorter than 20 (or whatever number you choose), so you'll probably want to get the line length and calculate it. I don't have any code samples for that committed to memory though.

  7. David says

    That is a very useful piece of code, thanks.

    Out of interest, how much additional coding would it take to check if a particular task already existed and update that task rather than creating a new one? e.g. any email coming from address aaa@bbb.com would cause the same task to be updated with the subject line of the email.

    • Diane Poremsky says

      You'd need to check search the existing tasks for a match, so it would require a bit of additional coding.

  8. Lee Edwards says

    CODE LIKE EMAIL TO TASK CAN ANYONE HELP?
    I have an Outlook add-in called Credenza which looks to be based on the Journal feature in OL , is there a way to modify the code so an email could be made into one of these Credenza cases automatically?
    Kind of like email to task just email to case that has a certain format and form?

    http://www.credenzasoft.com/Features/manage.html#

    • Diane Poremsky says

      Possibly. I don't use Credenza so I don't know for sure. The big thing is whether Outlook can save or move to the credenza store. It would probably be best to ask Credenza support.

  9. Jeffrey Gage says

    This works awesome and so grateful I was able to stumble across this tip.
    I have a requirement to move the task into a Task List linked to Sharepoint.
    Would you by chance know how to save the task into the sharepoint list instead of the regular list within Outlook?

    • Diane Poremsky says

      You need to use getfolderpath

      Add the two bolded lines to the code and change the name of the tasks folder
      Set SPSFolder = GetFolderPath("SharePoint ListsYour Site - Tasks")
      Set objTask = Application.CreateItem(olTaskItem)
      -- snipped code --
      objTask.Save
      objTask.Move SPSFolder

  10. Ross Dickerson says

    The code is great and worked the first time I tried it. I deleted the test tasks it created, but now it does not create new tasks when I run it. I have tried restarting Outlook, but it still fails. Restarting the computer, and it still fails. The code is still the same in VBA.

    I am in a corporate domain environment. Could that be disabling this functionality?

    Any ideas?

    • Diane Poremsky says

      If it worked in the test on the same machine, it's not the corp blocking it. Did you check the macro security settings? File, Options, Trust Center, Macro security in Outlook 2010 and 2013 or Tools, Macros, Security in older versions. Is the rule enabled?

  11. Jon Spector says

    I'd like to trigger this script by setting up a rule that runs whenever Outlook receives an email with certain words in the subject line. I am running the Outlook client on several computers, none of which is guaranteed to be running Outlook all the time (i.e. I use a laptop at work, and when I am travelling it is not on). Is the script stored on the Exchange server, so it will be available to run whenever it is triggered by the rule? If not - what happens when the rule triggers the script, but the script is on a machine which is not running?

    • Diane Poremsky says

      No, the script is not stored on the server - it's in Outlook and Outlook needs to be running to use it. The rules that use Run a script action are 'this machine only' rules so they only run on the specific computer.

  12. John says

    This script is 90% of what I have been trying to achieve.

    What do you need to add to the script to add the email attachment(s) to the task? This is the last 10%

    Thanks for the script!

    • Diane Poremsky says

      I have a script here somewhere that gets attachments... get the CopyAttachments function from the Copy appointments macro

      Stick this code before the objTask.save

      If Item.Attachments.Count > 0 Then
      CopyAttachments Item, objTask
      End If

  13. Diane Poremsky says

    BTW, if you just wanted ot add the email message as an attachment, you'd use
    Dim newAttachment As Outlook.Attachment

    Set newAttachment = objTask.Attachments.Add(Item, Outlook.OlAttachmentType.olEmbeddeditem)

  14. John says

    when I debug, I get: "Compile error: Sub or Function not defined" popup and
    "CopyAttachments" is highlighte.

    I'm not sure I'm inserting the two bits of code in the right place;

    The code:

    If Item.Attachments.Count > 0 Then
    CopyAttachments Item, objTask
    End If

    goes right before the .save statement in the "With objTask..." block

    but where does the CopyAttachments function from the copy appointments macro go?

    TIA,
    JH

    • Diane Poremsky says

      That error means you forgot to get the Copy Attachment function from this page

      Sub CopyAttachments(objSourceItem, objTargetItem)
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
      strPath = fldTemp.Path & ""
      For Each objAtt In objSourceItem.Attachments
      strFile = strPath & objAtt.FileName
      objAtt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
      fso.DeleteFile strFile
      Next

      Set fldTemp = Nothing
      Set fso = Nothing
      End Sub

  15. Michael says

    Dear Diane.
    I would first like to applaud your work - it is right on the point, clearly structured and helpful.
    I have been looking for a pragmatic approach to manage the "to-dos vs. tasks" problem in Outlook for months (if not years). Your solution goes very much into that direction, however I am still trying to figure out if there is a way to also solve the focus on flags and the automatization feature (e.g. with repetitively running script?). I have also found the following code which might be useful for an expert like you (http://xmindlook.net/xmindlook-sync/documentation/tips/email-to-task-conversion).
    I would be very greatful for your help!
    M

    Desired solution:
    Flagged emails shall be automatically (e.g. every couple minutes) converted into corresponding tasks
    Desired behavior:
    * for newly created task (placed in default task folder on exchange, mail itself and mail-attachments included as attachments in task, email subject set as task title, email receival date set as start date in task, other fields like category left empty (will be set manually later on))
    * for original mail (flag disabled (empty), text "[Task] " added at the beginning of the mail subject)

    • Diane Poremsky says

      Changing flags to task every few minutes won't work, outlook doesn't have a timer feature. You can trigger macros from reminders though - so it is possible if you need some automation. If you use a lot of reminders you can trigger it with any reminder, otherwise, you'll need to use a recurring task or appointment. (If you send a lot of mail, item send event could trigger the macro). In the case of the macro at the other site, where you need to affect synced items, triggering a macro by an event probably the best option.

      If you want to create the task as you flag an item, use an item change event that creates the task when the item is flagged. See the quick flag example here.

  16. Craig says

    Diane, you're a genius!

    Would there be a way to look in the body or subject of the email for Due Date data and enter that in the task?

    Many thanks for your work already.

  17. Sharon says

    Hi, this site is great! I wanted to use this code but I need it to run when I am not in the office (as I want to send myself emails which will create tasks) - I see from what you say that the code lives on the machine. So, looking at it from a new angle, is it possible to have a macro that runs when I launch Outlook which would check my inbox for emails with "Task" in the subject for example and create tasks when it finds one?

    I hope you can point me in the right direction! Thanks

    • Diane Poremsky says

      Yes, it would be possible to scan the mailbox on start up instead of using a rule to trigger a macro.

    • Diane Poremsky says

      Here is a version that checks new mail as it arrives in Outlook. It's really no different than using rules, but can handle processing a larger number of new messages better. It would also be possible to call a macro from Application_Startup, but that would only process existing email, not new mail as it arrives. Another option is using reminders to trigger a macro that checks the Inbox. This should run before rules, so any rule that adds categories or moves the message shouldn't affect it.

      This version looks for the word "Task:" at the beginning of a message, then creates a Task due tomorrow.

      Private Sub Application_Startup()
      Dim Ns As Outlook.NameSpace

      Set Ns = Application.GetNamespace("MAPI")
      Set oItems = Ns.GetDefaultFolder(olFolderInbox).Items

      End Sub

      Private Sub oItems_ItemAdd(ByVal Item As Object)
      If Left(Item.Subject, 5) = "Task:" Then
      Dim objTask As Outlook.TaskItem
      Set objTask = Application.CreateItem(olTaskItem)
      With objTask
      .Subject = Right(Item.Subject, Len(Item.Subject) - 6)
      .StartDate = Item.ReceivedTime + 1
      .Body = Item.Body
      .Save
      End With
      Item.Categories = "Made Task"
      Item.Save
      End If
      Set objTask = Nothing
      End Sub

  18. sarah m says

    So easy to use! Thanks. I also added a If unread = true condition so it would only apply the code to new, unread messages. otherwise it kept adding every message that met my conditions, even if they were old, my task list got very long!

  19. Terry T says

    Diane I love this macro. I just wish it would not delete the e-mail from the folder. Can you help me out?

    • Diane Poremsky says

      It should not be deleting the mail from the folder. At most, it will mark it read. Are you using a view that hides read messages?

      Are you using the rule method? If so, how does the Rule read?

  20. Tyler says

    Hi Diane -
    I'd like to save the tasks to a sub folder within the default tasks, in other words not on a Sharepoint server -
    Thank you

  21. Jack says

    Fantastic stuff - being a newbe when I go to create a rule, it doesn't give me the option of "run a script". what am I doing wrong? thx

    • Diane Poremsky says

      What type of email account do you use? Are you right clicking on a message or going to Rules and Alerts? If you right click, you need to click Advanced. Run a Script is near the bottom of the Actions list of the Rules Wizard.

  22. Brian says

    Is there a way to have the email as an attachment, but NOT have the body of the email pasted into the notes area of the task? I would rather keep that area clean for things I need to know about the task, plus the attachment is already there

    • Diane Poremsky says

      If you are using the VBA, Remove the .Body = Item.Body line and add
      Dim newAttachment As Outlook.Attachment
      Set newAttachment = objTask.Attachments.Add(Item, Outlook.OlAttachmentType.olEmbeddeditem)

  23. Brian says

    Perfect! I was able to stop the body text as well as get it to categorize and set dates the way i want. Next question, how can I combine this with the quickflag one you posted earlier? If I forget to BCC myself, I would like to be able to go to the the follow up flag and get the same result. How do I combine that code with what I've already done?

  24. moltra1 says

    Is there a way to copy the format of the email text to the task. i.e. if the email had tables in it.

    • Diane Poremsky says

      Tasks don't support HTMLBody, but you can try .Body = Item.HTMLBody. It should look as good (or bad) as dragging a message to the task folder to create a task.

  25. Ersula Washington says

    Hi Diane,

    I think I might be totally confused and I consider myself an advanced user of Microsoft Office. Since I don't have much experience in the way of writing scripts, is it possible to get a step-by-step of this process? I have been trying to convert my emails into a task by way of creating a rule. Currently, I have a rule setup to create a task but it's just the email sitting in my task folder. Please help!

    Ersula

    • Diane Poremsky says

      If it was moved to Tasks, it should be a copy of the message - the code adds the message body to the task body. I've added a tutorial to this page that shows how its done.

  26. Jaime says

    Can you provide code to the do the same thing, but to also set the DueDate at ReceivedTime + 2 WORK days? I've searched for hours and can't seem to find something I can work with...I am a novice to VB.

    • Diane Poremsky says

      You'll need to calculate the due date and check it against the weekend - this assumes Mon- Fri workweek - stick it in before with objtask and change the .startdate to .startdate = dayDue

      Dim dayDue
      dayDue = Item.ReceivedTime + 2
      If Weekday(dayDue) = vbSaturday Then
      dayDue = dayDue + 2
      Else
      If Weekday(dayDue) = vbSunday Then
      dayDue = dayDue + 1
      End If
      End If

  27. Jaime says

    In addition to the post above (on 7/12/13), is it possible to create something that would only execute the code if the email is an original email and not a reply or forward of an email? I plan to use this in conjunction with an Outlook rule to run this script if the Subject meets word based criteria. But I would only want it to run on the original email and not any subsequent related emails I might receive. Any help you can provide is much appreciated.

    • Diane Poremsky says

      You would need to filter for RE, FW:

      If Left(LCase(Item.Subject), 3) = "fw:" or Left(LCase(Item.Subject), 3) = "re:" Then
      Exit Sub
      Else
      ' do whatever

      end if

  28. David Deblauwe says

    Diane, how/what code would you include in the script to include the recipient/sender of the email to convert into the subject line of the task? Thanks, David

    • Diane Poremsky says

      Use .Subject = Item.Subject & " " & Item.SenderEmailAddress to add the sender's address.

  29. Chris says

    Hello, I followed these steps using the first code at the top of this page, last Friday. The rule worked as expected. Came in this morning, and now the script will not run. Everything else in the rule works (alert, pop up notice etc), but the task is not created. Outlook 2010

    • Diane Poremsky says

      That can be as simple as adding a second code block to the macro:

      Set objTask = Application.CreateItem(olTaskItem)
      With objTask
      .Subject = "Task 1" & item.Subject
      .StartDate = item.ReceivedTime
      .Body = item.Body
      .Categories = "Task1"
      .Save
      End With
      Set objTask = Nothing

    • Diane Poremsky says

      Add these two lines in the with statement - you can use an address or alias as long as outlook can resolve it, it will work.
      .Assign
      .Recipients.Add "email@address.com"

  30. Allen Hopson says

    Hello Diane!

    Your coding solves a big issue in my organizational management skills and I thank you! However, I have had the same experience Chris who posted on 9/16/13 - I have followed the steps using the first code at the top of the page - the rule WORKED - now the rule does NOT work - Everything else in the rule works (such as marking item as read), but the task is not created. I checked Macro Settings in Trust Center and the only thing that is checked is "Notifications for digitally signed macros, all other macros disabled". I am running Outlook 2013 (Office 365 Enterprise). Can you help me? I do not understand the security settings in Outlook. Thanks!

  31. Can Kefeli says

    Hi Diane,

    I used your code to create task for email with certain words in subject. Also ı want to modify due date of task with information in the email. I tried to use code below but it did not work. Please help me about it

    note: I checked "tarih" in excel and it gets correct date.

    The mail body of email is like below

    NOR-RONA-TRB-ANOP TASKS
    No HYPERLINK "http://kumluca/easyforms/data_details.php?CODE=RINL45F6UHYQ0OV92WZK&FIELD_NAME=No&VALUE=16"16
    Konu:
    2014 Main-Remote çalışması
    Açıklama:
    2014 te yapacağımızı main-remote dönüşümlerini planlayıp süreçleri başlatabilir misiniz?
    İstek Yapan:
    CAN KEFELI
    Toplantı T:

    Hedef T:
    2014-01-31 09:48
    Durum:
    ONAY_BKL
    Sorumlu:
    TCCISAHIN
    Öncelik:
    Normal
    Hafta:
    Y14W03
    Kayıt Tipi:
    Private
    Copyright © Turkcell

    THE CODE:

    Sub ConvertMailtoTask(Item As Outlook.MailItem)
    Dim tarih As Date
    Dim mesajicerik As String
    Dim tarihyeri As Integer
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Item.Body
    mesajicerik = objTask.Body
    tarihyeri = InStr(1, mesajicerik, "Hedef T: ")
    tarih = DateSerial(Mid(mesajicerik, tarihyeri + 11, 4), Mid(mesajicerik, tarihyeri + 16, 2), Mid(mesajicerik, tarihyeri + 19, 2))
    .DueDate = tarih
    .Save

    End With
    Set objTask = Nothing
    End Sub

    • Diane Poremsky says

      Using your text sample as entered here -

      No space:
      Hedef T:
      2014-01-31 09:48

      Space:
      tarihyeri = InStr(1, mesajicerik, "Hedef T: ")

      When remove the space, the due date is the date at Hedef T:.

      If that isn't the problem, we know the dateserial line is good.

  32. Can Kefeli says

    Hi Diane,

    thank for the code it work fine but ı want to modify created task due date with information in email. so ı wrote below code but it did not work.

    I checked the variable "tarih" in excel it gets correct date information from email body.Please help me

    Sub ConvertMailtoTask(Item As Outlook.MailItem)
    Dim tarih As Date
    Dim mesajicerik As String
    Dim tarihyeri As Integer
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Item.Body
    mesajicerik = objTask.Body
    tarihyeri = InStr(1, mesajicerik, "Hedef T: ")
    tarih = DateSerial(Mid(mesajicerik, tarihyeri + 11, 4), Mid(mesajicerik, tarihyeri + 16, 2), Mid(mesajicerik, tarihyeri + 19, 2))
    .DueDate = tarih
    .Save

    End With
    Set objTask = Nothing
    End Sub

    • Diane Poremsky says

      Can you post a sample of the message text you're searching? Is the date the last line of the message?

  33. Can Kefeli says

    Hi again,
    when ı used the code below, it created the tasks buts due date of task is 30.7.2019 so I think ı could not get the right text parts of task body. Is there a way to see which parts I get from the body with code?

    THE CODE

    Sub ConvertMailtoTask(Item As Outlook.MailItem)
    Dim tarih As Date
    Dim mesajicerik As String
    Dim tarihyeri As Integer
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Item.Body
    mesajicerik = objTask.Body
    tarihyeri = InStr(1, mesajicerik, "Hedef T:")
    tarih = DateSerial(Mid(mesajicerik, tarihyeri + 10, 4), Mid(mesajicerik, tarihyeri + 15, 2), Mid(mesajicerik, tarihyeri + 18, 2))
    .DueDate = tarih
    .Save

    End With
    Set objTask = Nothing
    End Sub

    TASK BODY

    NOR-RONA-TRB-ANOP TASKS
    No HYPERLINK "http://kumluca/easyforms/data_details.php?CODE=RINL45F6UHYQ0OV92WZK&FIELD_NAME=No&VALUE=16"16
    Konu:
    2014 Main-Remote çalışması
    Açıklama:
    2014 te yapacağımızı main-remote dönüşümlerini planlayıp süreçleri başlatabilir misiniz?
    İstek Yapan:
    CAN KEFELI
    Toplantı T:

    Hedef T:
    2014-01-31 09:48
    Durum:
    ONAY_BKL
    Sorumlu:
    TCCISAHIN
    Öncelik:
    Normal
    Hafta:
    Y14W03
    Kayıt Tipi:
    Private
    Copyright © Turkcell

    • Diane Poremsky says

      use debug.print after you set the variables - show the Immediate windows (Ctrl+G or the view menu) and see if the values are correct. The only thing I can think is that the date serial format is not putting the numbers in the correct order - it worked here with the US date format (mm/dd/yyyy).

      tarih = DateSerial(Mid(mesajicerik, tarihyeri + 10, 4), Mid(mesajicerik, tarihyeri + 15, 2), Mid(mesajicerik, tarihyeri + 18, 2))
      debug.print tarih
      debug.print Mid(mesajicerik, tarihyeri + 10, 4)
      debug.print Mid(mesajicerik, tarihyeri + 15, 2)
      debug.print Mid(mesajicerik, tarihyeri + 18, 2)

  34. Can Kefeli says

    Hi again, I solve the issue with tihs code, thanks. But I could not set reminder active, it changes reminder date but tick near remider was not checked, how can ı do it?

    Sub ConvertMailtoTask(Item As Outlook.MailItem)

    Dim mesajicerik As String
    Dim tarihyeri As Integer
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Item.Body
    mesajicerik = objTask.Body
    tarihyeri = InStr(1, mesajicerik, "Hedef T:")
    .DueDate = Mid(mesajicerik, tarihyeri + 17, 2) & "." & Mid(mesajicerik, tarihyeri + 20, 2) & "." & Mid(mesajicerik, tarihyeri + 12, 4)
    .ReminderTime = .DueDate - 1
    .Save

    End With
    Set objTask = Nothing
    End Sub

  35. Chris says

    Hi, I've been using this excellent code successfully for some while. Now I would like to make one small change I'm unsure how to do it. I have created a task form of my own and saved it personal forms library, I have called it "my task". How can I adapt the code to use my form instead of the default task form.

    I think I know what I need to change, but don't know what to change it to!!!

    • Diane Poremsky says

      This line: Set objTask = Application.CreateItem(olTaskItem)
      Needs to be changed to
      Set objTask = Application.CreateItemFromTemplate("C:\path\to\test.oft")

    • Diane Poremsky says

      I thought I replied earlier. :( published forms are called using Add - Items.Add("ipm.tasks.another-task-final")

  36. Nick says

    So I followed all the instructions, however when I try to run the rule nothing happens in Outlook, and when I try to force the rule to run it (the "loading window") just disappears.

    • Diane Poremsky says

      Is macro security set to low? Try adding MsgBox "it works!" as the first line of the macro - this will tell us if the macro is running.

  37. Nick Harger (@nickharger) says

    It still has the same error. It worked for a few moments before the "loading window" goes back to disappearing.

    • Diane Poremsky says

      What is the complete macro you are trying to use? I'll try to repro the error.

  38. Nick Harger (@nickharger) says

    Here is what I have in VB:

    It Works!
    Sub ConvertMailtoTask(Item As Outlook.MailItem)
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime
    .Body = Blank
    Dim newAttachment As Outlook.Attachment
    Set newAttachment = objTask.Attachments.Add _
    (Item, Outlook.OlAttachmentType.olEmbeddeditem)
    .Save
    End With
    Set objTask = Nothing
    End Sub

  39. cir0net says

    Hello,

    Thank you all, this VBA Code used to work great for us. We used it with creating / syncing the task to a linked Office 365 Sharepoint-Tasklist.
    Too bad, since the Office 365 2013 Upgrade it´s not working any more. MS changed the sync to outlook feature in a big way. Now new task are created/synced always first as personal tasks and the GetFolderPath function (see below) to move the tasks to the sharepoint list is not working anymore.
    Looking for some help how to make this work again. Is there a way to add a new task-item to sharepoint task list from VBA?
    Thank you in advance for any advice and code samples.

    Move task to Sharepoint List:
    Set SPSFolder = GetFolderPath("SharePoint ListsYour Site - Tasks")
    Set objTask = Application.CreateItem(olTaskItem)
    -- snipped code --
    objTask.Save
    objTask.Move SPSFolder

    • Diane Poremsky says

      It should still work - do you get any error messages?

      Or... Rather than using createitem and Move, how about using item.add and save it to the folder directly?
      Set objTask = spsfolder.Items.Add(olTaskItem)

  40. Mathieu Cote says

    This is great and I want it to work. I copy/pasted the code in the article and get a syntax error... does anyone know what to do with this? It happens for the first line of code (Sub ConvertMailtoTask(Item As Outlook.MailItem))

    • Diane Poremsky says

      Anything colored yellow or red? Syntax error often means a typo of some sort in the code.

  41. Curt Faulk says

    Diane:

    Diane:
    First, the help you provide and columns you write are excellent. And that you have time to reply and provide detailed responses to everyone is amazing. I can't thank you enough.

    I would like to tweak the code a bit so that the resulting task includes the From/Sent/To/Subject info from the email header above the body text of the email. In other words, it would appear just as if you selected a received message, clicked forward, and copied all of the text below the solid line that Outlook inserts above the From: line.

    I'm using both the Outlook Rule to "Create a task from an Email" and the Macro for creating a task from a selected message and will be using any suggestions you provide in each. Thank you very much!

    • Diane Poremsky says

      Well, considering i have 40 comments in the queue waiting for replies... I'm not that good. :)

      You'd change .Body = Item.Body

      to something like .body = item.subject & vbcrlf & item.sender & vbcrlf & item.to & vbcrlf & " -------------- " & vbcrlf & item.body

      You may need to use two vbcrlf together and may want to assign the fields to strings then put the strings together.

  42. Curt Faulk says

    Acuatlly, I figured it out be looking at your code and a little deductive reasoning, here is what worked:

    .Body = "From: " & Item.Sender & vbCrLf & "Sent: " & Item.ReceivedTime & vbCrLf & "To: " & Item.To & vbCrLf & "Subject: " & Item.Subject & vbCrLf & " ------------------------------------------ " & vbCrLf & Item.Body

    Works great. Thanks! (and you're doing a great job keeping up!)

    • Diane Poremsky says

      Yes, you'll use appointmentitem instead of taskitem and will use different fields.

  43. Haik says

    Thanks Diane, I combined some code I found on the internet and tested. This one works with outlook appoinments from email as a script with rules:

    Sub MakeAppointmentFromEmail(item As Outlook.MailItem)
    Dim objAppt As Outlook.AppointmentItem
    Dim objMail As Outlook.MailItem
    Dim strID As String
    strID = item.EntryID

    Set objAppt = Application.CreateItem(olAppointmentItem)

    Set objMail = Application.Session.GetItemFromID(strID)
    objAppt.Subject = objMail.Subject
    objAppt.Start = objMail.ReceivedTime
    objAppt.ReminderSet = True
    objAppt.Mileage = 30
    objAppt.Save

    Set objAppt = Nothing
    Set objMail = Nothing

    End Sub

  44. Haik says

    Thank you, it works. Another question: Is it possible to read and write to userdefinedfields from code? For instance a field I called "OneWayTravelDistance". How do I access this userfield from script? Do you have an example?

    • Diane Poremsky says

      Yes, you can do this. You need to refer to the field as .UserProperties("customfield")

  45. Mike Mc says

    Hi Diane,

    You have very useful information on your site.

    I am wondering if it's possible to get a cell value, say A1, from Excel and set it as the objTask.DueDate and the objTask.ReminderTime. I would assume the format in the cell would have to be mm/dd/yyyy hh/mm. Your help would be greatly appreciated.

  46. Kevin Stanlay says

    Hi Diane,
    You have very useful information on the site and I thank you for that.

    I was wondering if it's possible to grab a cell value, say cell A1, from Excel and set it as the objTask.DueDate and objTask.ReminderTime. I think the cell format would have to be like mm/dd/yyyy hh:mm. I'm not sure how to call the Excel sheet and what to set objTask.DueDate and objTask.ReminderTime equal to. Your help is greatly appreciated.

  47. Curt Faulk says

    Hi, Diane. Thank you for all your help. I can't seem to get the copy attachments to work.

    Here is my code, I just can't figure out what I've done wrong.

    Sub CopyAttachments(objSourceItem, objTargetItem)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
    strPath = fldTemp.Path & "\"
    For Each objAtt In objSourceItem.Attachments
    strFile = strPath & objAtt.FileName
    objAtt.SaveAsFile strFile
    objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
    fso.DeleteFile strFile
    Next

    Set fldTemp = Nothing
    Set fso = Nothing
    End Sub
    Sub ConvertSelectedMailtoTask()
    Dim objTask As Outlook.TaskItem
    Dim objMail As Outlook.MailItem

    Set objTask = Application.CreateItem(olTaskItem)

    For Each objMail In Application.ActiveExplorer.Selection

    With objTask
    .Subject = objMail.Subject
    .StartDate = objMail.ReceivedTime
    .Body = "From: " & objMail.Sender & vbCrLf & "Sent: " & objMail.ReceivedTime & vbCrLf & "To: " & objMail.To & vbCrLf & "Subject: " & objMail.Subject & vbCrLf & " ------------------------------------------ " & vbCrLf & objMail.Body
    .Categories = "A - Must Do"
    If Item.Attachments.Count > 0 Then
    CopyAttachments Item, objTask
    End If
    .Save
    End With

    Next
    Set objTask = Nothing
    Set objMail = Nothing
    End Sub

    • Diane Poremsky says

      You need to use the correct variable for your objects:
      If objMail.Attachments.Count > 0 Then
      CopyAttachments objMail, objTask
      End If

  48. Bennett says

    Thanks for the help, this works great. I have added custom fields to the task form and renamed it. When I run this script though it always creates the task using the default task form. Is there away I can have it create the task using the custom form I have already made?

    • Diane Poremsky says

      Yes, you need to replace the code that creates the new task with code that uses the template.

      Set objTask = Application.CreateItemFromTemplate("template.oft")
      If it's a published form, try the published form name "ipm.task.mine", if that doesn't work , use item.add:
      Set objtask = Items.Add("ipm.note.name")

    • Bennett says

      The replacement code doesn't seem to be working. The custom task form is an ipm.task file but either doesn't work or gives me an error when I try the new code. Any tips?

    • Diane Poremsky says

      Oops. You need to tell it which folder to use.
      Dim oFolder As Outlook.Folder
      Set oFolder = Application.Session.GetDefaultFolder(olFolderTasks)
      Set objTask = oFolder.Items.Add("IPM.Task.task with p2")

    • Bennett says

      I have a follow up question, what would the code look like to set the due date to "None"? Thanks,

  49. Bennett says

    I am having some trouble. When I try using the first line of code it prompts me to enter a file path after I enter the published form name. I have the custom form saved in my personal form library but I am not sure what the file path is. When I use the Item.add code it highlights red when I try and save the code and gives me a syntax error when I run it.

    Also I had the word "with" in the name of the custom task form. This appeared to give me issues when running the code. I renamed it removing "with", and I stopped getting that error. Should I not be using "with" in custom form names?
    Thanks,

    • Diane Poremsky says

      Interesting on using With in the file names - I'm not sure why it wouldn't work, while with is used in VBA, you'd have it wrapped in quotes.

  50. William Larson says

    Thanks for this tutorial,
    I am trying to create 2 tasks from 1 email with the code you provided and a rule to check mail when it arrives. I have been unsuccessful in getting the second task created. Here is my latest attempt (code below). Can you please let me know what I am doing wrong? I have placed it in ThisOutlookSession. The first task is created but I cant get the code to create the second task. Thanks in advance for your help.

    Sub ConvertMailtoTask(Item As Outlook.MailItem)
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
    .Subject = Item.Subject
    .StartDate = Item.ReceivedTime + 1
    .DueDate = Item.ReceivedTime + 1
    .Body = Item.Body
    .ReminderSet = True
    .ReminderTime = objTask.DueDate + 0.4375
    .Save
    End With
    Set objTask = Nothing
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
    .Subject = "Task 1" & objMail.Subject
    .StartDate = Item.ReceivedTime + 1
    .DueDate = Item.ReceivedTime + 1
    .Body = Item.Body
    .ReminderSet = True
    .ReminderTime = objTask.DueDate + 0.8125
    .Save
    End With
    Set objTask = Nothing

    End Sub

    • Diane Poremsky says

      This should be item, not objmail: Subject = "Task 1" & objMail.Subject. Otherwise, it is working fine here - one task has a reminder in the morning, one in the evening.

    • William Larson says

      Thank you for your very kind assistance,
      I will make the change you suggested. You may want to do the same to the response you posted to Dave September 26, 2013 at 9:11 am. That is where I found the code to add the additional task. Thanks again for your help, Keep up the fine work.

    • Diane Poremsky says

      Thanks, I will fix that code too. I looked at your code quite a few times before I noticed it.

  51. Russ B. says

    All of this has been amazing! Thank you! If I wanted to move the email that I created the task from to a sub folder under the inbox what would I need to add? We are non-profit specialty hospital and this simple script has become invaluable to us. Thank you for your generosity in sharing your knowledge!

    • Diane Poremsky says

      After the task is created add this line:
      objMail.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Converted to Tasks"))

      change the folder name and change objMail to item if you're using the first macro with 'item as mailitem' in it.

  52. Kyle says

    Hey Guys,

    Sorry to keep this going.

    If I have 2 email accounts on one outlook, how can I specify which account to assign the task to?

    For example,

    Default email is Email1@email.com

    I need to create the task for Email2@email.com

    Both emails are set up on the same outlook. I would like to avoid emailing it, which I cannot seem to get to work either way.

    • Diane Poremsky says

      You need to use the code in the section for "Save to a different tasks folder" and use the location of the second tasks folder in the code then copy it. It's also possible to set the folder and Add the task to it - either method works well but I don't have a sample on this page that uses Add.

      if you want to use a rule and only turn mail from account 2 into tasks, you can choose the sending account in the rule.

    • Kyle says

      Thank you for responding.

      I tried changing the account sending the rule, and it still defaulted to email1.

      I will research further on how to find how to set and add the outlook object to it in outlook vba somewhere. Thank you for pointing me in the right direction. If I am successful, I will post my findings.

  53. kris says

    Hi,
    I want to use email alert sent from sharepoint to convert to a task. I think the Sub ConvertMailtoTask(Item As Outlook.MailItem) in the macro is only converting mail to task. Can you please help.

    • Diane Poremsky says

      This is probably the problem: Item As Outlook.MailItem - I'm guessing the notification is not seen as a mail item, it's program a report. Try item as object instead.

      Sub ConvertMailtoTask(Item As object)

  54. John says

    I've cobbled together an Outlook (2010) email-to-task VBA script that does exactly what I need it to EXCEPT for one thing: I want to copy the name of the Sender to the "Contacts..." field at the bottom of the Task window. Any tips?

    Here's my script:
    Sub TestCopyFullBody()
    Dim objMsg As Outlook.MailItem
    Dim objTask As Outlook.TaskItem

    Set objMsg = Application.ActiveExplorer.Selection(1)
    Set objTask = Application.CreateItem(olTaskItem)
    Call CopyFullBody(objMsg, objTask)
    objTask.Display
    objTask.Categories = objMsg.Categories
    objTask.Subject = objMsg.Subject & " (from: " & objMsg.Sender & ")"
    objTask.Attachments.Add objMsg

    Set objMsg = Nothing
    Set objTask = Nothing
    End Sub
    Sub CopyFullBody(sourceItem As Object, targetItem As Object)
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    Dim objDoc2 As Word.Document
    Dim objSel2 As Word.Selection
    On Error Resume Next
    ' get a Word.Selection from the source item
    Set objDoc = sourceItem.GetInspector.WordEditor
    If Not objDoc Is Nothing Then
    Set objSel = objDoc.Windows(1).Selection
    objSel.WholeStory
    objSel.Copy
    Set objDoc2 = targetItem.GetInspector.WordEditor
    If Not objDoc2 Is Nothing Then
    Set objSel2 = objDoc2.Windows(1).Selection
    objSel2.PasteAndFormat wdPasteDefault
    Else
    MsgBox "Could not get Word.Document for " & _
    targetItem.Subject
    End If
    Else
    MsgBox "Could not get Word.Document for " & _
    sourceItem.Subject
    End If
    Set objDoc = Nothing
    Set objSel = Nothing
    Set objDoc2 = Nothing
    Set objSel2 = Nothing
    End Sub

    • John says

      The Contact linking field is showing (I've enabled it and I'm on 2010).

      Forget about validating that the contact exists and then creating it if not... would you please show me how to add the contact?

    • Diane Poremsky says

      You can't just add an address - you need to find the contact. If you use just a name or address, outlook errors with 'item cannot be found'

      objtask.Links.Add objmsg.sender

      if you create the contact -
      Set objContact = Application.CreateItem(olContactItem)
      With objContact
      .FullName = objMsg.Sender
      .Email1Address = objMsg.SenderEmailAddress
      .Save
      End With

      then use

      objtask.Links.Add objContact

      it works. better would be to attempt to resolve the address and create it if outlook can't resolve.

    • John says

      "You can't just add an address - you need to find the contact."

      Diane - thanks for your help so far. Something I didn't state in my original request was that almost 100% of the emails I'm dealing with are from internal personnel, over Exchange. I apologize for not being clear on this.

      So, I guess my clarified requirements are now:

      1. If sender is in Contacts, add contact to objTask.Links

      And then:
      * If sender is not in Contacts, create in Contacts, and then add contact to ObjTask.Links

      Or, even better:
      * If sender is not in Contacts, find sender in Exchange address book, add (create?) to Contacts, and then add contact to ObjTask.Links

    • Diane Poremsky says

      If outlook is set to resolve the GAL first, it should work (I think!)... i don't have any code handy though.

    • Diane Poremsky says

      this snippet messy but it seems to work ok on contacts - the stuff between the === is the code to add to your code.

      Call CopyFullBody(objMsg, objTask)

      '=====================
      Dim objContacts As Outlook.folder
      Dim objNS As Outlook.NameSpace
      Dim objProperty As Outlook.UserProperty

      Set objNS = Application.GetNamespace("MAPI")
      Set objContacts = objNS.GetDefaultFolder(olFolderContacts)
      Set objContact = objContacts.Items.Find("[email1address] = " & objMsg.SenderEmailAddress)
      If TypeName(objContact) = "Nothing" Then

      Set objContact = Application.CreateItem(olContactItem)

      With objContact
      .FullName = objMsg.Sender
      .Email1Address = objMsg.SenderEmailAddress
      .Save
      End With

      End If
      '=================

      With objTask
      .Display
      .Categories = objMsg.Categories
      .Subject = objMsg.Subject & " (from: " & objMsg.Sender & ")"
      .Attachments.Add objMsg
      .Links.Add objContact
      End With

  55. David Nestlebush says

    Hello,

    I'm having a couple of issues with the macro below. When I assign it to someone, it is making them the owner (I would like to be the owner and have it assigned to the person), and the start date is being entered 12/31/1899 (and is showing 5000 days late). I'd also like to make sure I have the "Copy Attachement" portion correct.

    Sub ConvertMailtoTask2()
    Dim objTask As Outlook.TaskItem
    Dim objMail As Outlook.MailItem

    Set objTask = Application.CreateItem(olTaskItem)
    For Each objMail In Application.ActiveExplorer.Selection

    objTask.Assign.Recipients.Add "abcde@abcde.com"
    objTask.Subject = objMail.Subject
    objTask.StartDate = ReceivedTime
    objTask.DueDate = Date + 1.5
    objTask.ReminderSet = True
    objTask.ReminderTime = objTask.DueDate - 0.25 ' remind at 6 AM 1 days from now
    objTask.Body = objMail.Body
    If Item.Attachments.Count > 0 Then
    CopyAttachments Item, objTask
    End If
    objTask.Send

    Next
    Set objTask = Nothing
    Set objMail = Nothing
    End Sub

    Sub CopyAttachments(objSourceItem, objTargetItem)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
    strPath = fldTemp.Path & "\"
    For Each objAtt In objSourceItem.Attachments
    strFile = strPath & objAtt.FileName
    objAtt.SaveAsFile strFile
    objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
    fso.DeleteFile strFile
    Next

    Set fldTemp = Nothing
    Set fso = Nothing
    End Sub

    • Diane Poremsky says

      The attachment lines need to reference the mail object:
      If objMail.Attachments.Count > 0 Then
      CopyAttachments objMail, objTask
      End If

      you also need the mail object on received time - outlook doesn't understand it and makes up a date. :)
      objTask.StartDate = objmail.ReceivedTime

      plain old "Date" or "Time" are VB functions and use the current date or time, the same as Today() or Now().

  56. Russ B. says

    Thanks for your previous assistance. I have one more question... I get emails that have attached tasks to them, Right now I drag the attachment to the task button then up to the Task folder to get it on my task list. Is there a quick script that I can execute that will take the attachment and move it to my task list? I plan to execute such a script as part of the rule that already grabs these emails and moves them to a specific folder. Any ideas or sample script will be greatly appreciated. Thank you!

    • Diane Poremsky says

      You'd create a new task and save then insert the attachment - the code should all be on this page, it just needs put together. Attachment code is under the working with attachment section.

  57. Thiago says

    Hello Diane,

    Congratulations for your code and for share it with us.

    I'm facing a situation here which I want to create a task from a SENT message using a rule. I can only run a script from a message I receive. Is it possible to run a script from a sent message, using a rule?

    Thanks in advance.

    • Thiago says

      Thanks a lot Diane! I did it successfully!

      Now I will work to manage the different created tasks to avoid duplication of tasks after sending an email.

      I'm aiming to update an associated task instead of creating a new one.

      The code I've developed is:

      Dim WithEvents sentItems As Outlook.Items

      Private Sub Application_Startup()
      Dim ns As Outlook.NameSpace
      Dim fld As Outlook.MAPIFolder
      Set ns = Application.GetNamespace("MAPI")
      Set fld = ns.GetDefaultFolder(olFolderSentMail)
      Set sentItems = fld.Items
      Set fld = Nothing
      Set ns = Nothing
      Dim TextPos As Integer
      Dim TextPosForCco As Integer
      Dim ResponseDays As Integer
      End Sub

      Private Sub sentItems_ItemAdd(ByVal Item As Object)
      ResponseDays = 3

      If Item.Class = olMail Then
      Set objMsg = Item
      Dim objTask As Outlook.TaskItem
      Set objTask = Application.CreateItem(olTaskItem)
      With objTask
      .Subject = Item.Subject
      .Body = Item.Body
      .Categories = "SI's"
      .StartDate = Date
      .DueDate = Date + ResponseDays
      .ReminderTime = Date + ResponseDays
      .ReminderSet = True
      .Save
      End With
      Set objTask = Nothing
      End If
      End If
      End Sub

    • Thiago says

      Hello Diane,

      Sorry to be disturbing you, but I'm not being able to get the current existing tasks inside my Outlook. I'm trying to use, for example:

      Dim fldTasks As Outlook.MAPIFolder
      Dim tasksItems As Outlook.Items
      Set fldTasks = ns.GetDefaultFolder(olFolderTasks)
      Set tasksItems = fldTasks.Items

      I've also tried to use:

      Set fldTasks = ns.GetDefaultFolder(olFolderTasks).Parent.Folders("Tasks")

      But both of these options returned an empty variable, so I cannot loop into the current tasks I have.

      Do you know what can be happening in this case?

      Best Regards

    • Diane Poremsky says

      This won't work: Set fldTasks = ns.GetDefaultFolder(olFolderTasks).Parent.Folders("Tasks")

      you only need Set fldTasks = ns.GetDefaultFolder(olFolderTasks)
      Try changing this: Dim tasksItems As Outlook.taskitem
      if you have a second tasks folder at the same level as the default tasks, you'd use Set fldTasks = ns.GetDefaultFolder(olFolderTasks).parent.folders("foldername")

      Do you have more than one account in the profile? If the tasks is in a different data file, you'll need to get the folder using a different method.

  58. Matthew Schwarz says

    Hi. Your site and your examples are very useful (and even fun to read). I have a question: I want to do something similar to above except I want to be able to flag a message for follow-up, and then get a prompt to rename the task to something else. As a super bonus, I would like a prompt to get the flag date picker (choose a start and due date).
    This would be useful for example if I sent an email requirement to a delegate and I bcc myself, then I'd have a rule to catch all incoming bccs and automatically flag them and give me a choice of due date and also let me customize the task title.
    Or if I have an email that requires action from me I could use this script to flag it with a choice of start date and then prompt to let me customize the task title.
    Thank you for any attention you can provide.

    • Diane Poremsky says

      Asking for a subject is simple -
      Dim strSubject As String
      strSubject = InputBox("enter new subject")

      then replace the subject = item.subject line with this:
      .Subject = strSubject

      Date pickers are more difficult. It would be easier to ask for a date in an inputbox and pass the value.

  59. Alexis Hernandez says

    Hello Diane,

    This seems to have worked for me however when doing tests I'm not receiving a 'completed' status report. When manually creating a task, there's a check box that needs to be selected in order to receive updates... is there a way to get this into a code? I hope you can help. thanks so much in advance.

    • Diane Poremsky says

      That is only available if you create a task request - but yes, it can be set using VBA AFAIK. When you use the following snippet in the macro, it creates a task request and both keep an updated copy and send status report are checked.
      With objTask
      .Assign
      .Recipients.Add "drcp@cdolive.com"
      .Subject = Item.Subject
      .StartDate = Item.ReceivedTime
      .Body = Item.Body
      '.Send
      .Display
      End With

  60. Alexis Hernandez says

    Thanks for the quick reply! However, I tried this as stated in your original notes. It is not sending a status report to the sender from the recipient once the task is marked complete. I need the 'Completed status report' to be sent to the sender.

    • Diane Poremsky says

      So you want to create a task from an email then send the person who sent you the email a completed status report?

  61. Alexis Hernandez says

    yes, that's exactly what I need. I tried researching how to format my own codes and cant seem to find how... any suggestions?

    • Diane Poremsky says

      Because the task wasn't assigned by that person, you can't do it using the automated feature in Tasks, but you could use a macro to send an update message when its marked complete.

    • Alexis Hernandez says

      Okay. I understand. 2 questions... Is there a code I can use to get the task assigned by the sender so that when I mark it complete the status report gets sent to them? Or would it be best to have the macro send an updated message? We are looking for something to be automatic, preferably to run on a script

    • Diane Poremsky says

      i don't think you can set the task to be from the sender, it would be easier to generate an email. The email can be automatic - it'll look for a task to be marked compete and kick in when that happens.

    • Alexis Hernandez says

      I may be very confused at this point.... I took the code sample you made and entered both mine and my supervisors emails, when I ran a test nothing happened. heres the code below.... what am I doing wrong? should the create task go above the create mail item?

      I receive emails with the subject "Incomplete task notification" from Danielle@century21city.net, frontdesk@century21city.com & century21city@att.net. I'd like all of those emails to be turned into tasks upon receipt into my inbox. Then once I mark those tasks complete, I want an automatic email with the original emails subject (so that the message pairs in the inbox under the correct thread) to be sent to Danielle@century21city.net- saying "task completed". how can I turn this into a macro to run a script.

      I hate to ask for so much help, but I've been stuck on this for quite sometime. Can you refer me to a website that will help me build macro codes on my own?

      '** This goes in thisoutlooksession
      Public WithEvents OlItems As Outlook.Items

      Public Sub Initialize_handler()
      Set olItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderTasks).Items
      End Sub

      Private Sub OlItems_ItemChange(ByVal Item As Object)

      If Item.Complete = True And Item.IsRecurring = False Then

      If InStr(1, Item.Categories, "Completed") = False Then

      With Item
      .Categories = "Completed" & .Categories
      .Save
      End With

      Dim objMsg As MailItem
      Set objMsg = Application.CreateItem(olMailItem)
      With objMsg
      .To = Item.UserProperties("danielle@century21city.net")
      .Subject = "This is the subject"
      .BodyFormat = olFormatPlain ' send plain text message
      .Body = "Whatever"
      .Display 'or .send
      End With

      Set objMsg = Nothing

      End If
      End If

      End Sub

      '**** Change script to set a custom field
      Sub ConvertMailtoTask(Item As Outlook.MailItem)
      Dim objTask As Outlook.TaskItem
      Dim objProp As Outlook.UserProperty
      Dim strAddr As String

      strAddr = Item.SenderEmailAddress

      Set objTask = Application.CreateItem(olTaskItem)

      With objTask
      Set objProp = .UserProperties.Add("alexis@century21city.com", olText, True)
      objProp.Value = strAddr
      .Subject = Item.Subject
      .StartDate = Item.ReceivedTime
      .Body = Item.Body
      .Save
      End With
      Set objTask = Nothing
      End Sub

    • Diane Poremsky says

      if all responses will go to one address, it makes it easier - you won't need to set the user property.

      Use this in the sub that sends the 'task is complete' message:
      With objMsg
      .To = "alexis@century21city.com"
      .Subject = Item.Subject 'gets task subject
      .Body = "Task Completed"
      .Display ' use .send to automatically send it
      End With

      and this to create the task - it should be what you used before:
      Sub ConvertMailtoTask(Item As Outlook.MailItem)
      Dim objTask As Outlook.TaskItem

      Set objTask = Application.CreateItem(olTaskItem)

      With objTask
      .Subject = Item.Subject
      .StartDate = Item.ReceivedTime
      .Body = Item.Body
      .Save
      End With
      Set objTask = Nothing
      End Sub

      There aren't many sites that will help you build macros - most expect you to have some knowledge of what you are doing so they aren't necessarily friendly towards beginners. msdn.microsoft.com and outlookcode.com are good resources, outlookforums.com, stackexchange.com and the MSDN forums are good if you have a question.

  62. Alita says

    I am trying to make sure I have the basic email and copy attachment to task code right. Could you please copy it all together for me?

  63. Jack Coole says

    Hi,

    First, Thanks for the tutorial! Was VERY helpful!

    Second. At the moment we are running it by having a generic e-mail address for our IT department which people can e-mail with their problems and issues. When the account receives an e-mail it then runs your script and sends both me and my colleague a task request. The only problem is that when we're filling out the task of where we're up-to, we cannot view the extra information that the other has written into the task because we're not the owner of the task. This means going onto the IT e-mail address and filling it out that way. As sometimes we're unable to do this I was just wondering if there was a way to update the task information without sending a task update which will just send out a new task request, leading to multiple tasks about the same job.

    Do you have any ideas on how it could be run?

    • Diane PoremskyDiane Poremsky says

      Offhand, no, I can't think of a way to do this.

      I worked on a task update macro that looked for specially crafted subjects and processed the messages as task updates. I don't know if something like that would work for you.

  64. Bennett says

    I would like to move some select parts of the body of the email into standard and custom fields of the task. The body of the email is entirely in the form of a table (Several rows and 2 columns). Is this possible? If so what would the code look like?

  65. Jatin says

    Hi there,

    What I am trying to do is create an calendar event from mail subject line as below.
    If I receive any mail with subject line as Due Date:01/01/2015 it should create a event in calendar and also alert me whenever that date and time occurs.
    Is this possible by rule or macro? Any help would be much appreciated.

    Thanks,
    Jatin

    • Diane PoremskyDiane Poremsky says

      You can't use a rule by itself, but can use a script with a rule to find messages which might match then process it more with a script. A regex example is here: http://www.slipstick.com/developer/run-a-script-rule-autoreply-using-a-template/

      I didn't test it, but this should be close:

      Sub ConvertMailtoTask(Item As Outlook.MailItem)
      Dim objAppt As Outlook.appointmentItem
      Dim Reg1 As Object
      Dim M1 As Object
      Dim M As Object
      Dim strDate
      Set Reg1 = CreateObject("VBScript.RegExp")

      With Reg1
      .Pattern = "Due Date[:]([\d/]*)"
      End With

      If Reg1.Test(Item.subject) Then

      Set M1 = Reg1.Execute(Item.Body)
      For Each M In M1
      strDate = M.SubMatches(0)
      Next
      End If

      Set objAppt = Application.CreateItem(olappointmentItem)
      With objAppt
      .Subject = Item.Subject
      .StartDate = strdate
      .Body = Item.Body
      .ReminderSet = True
      .ReminderTime = strdate
      .Save
      End With
      Set objappt = Nothing
      End Sub

  66. Sumeet says

    Hi Diane
    thanks for this amazing post
    while it has been asked previously in this chain whether the script can run when outlook is offline; is there absolutely no way that they script be stored in the mail server and emails get converted to tasks when one is on the move?
    pls do let know
    thanks
    sumeet

Leave a Reply

Please post long or more complicated questions at OutlookForums by Slipstick.com.

If the Post Comment button disappears, press your Tab key.