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, you need to use the CopyAttachments, added to the end of the task macro.
Add this code before the .save
If Item.Attachments.Count > 0 Then CopyAttachments Item, objTask End If
Then this after the end of the first macro:
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
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 = SPSFolder.Items.Add(olTaskItem) ' do whatever ' objTask.Save
Create the rule
Create 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. Simply change the Set objMail line to Set objMail = GetCurrentItem() and get the GetCurrentItem function from "Outlook VBA: Work with Open Item or Selected Item".
To use, select the message and run the macro.
Sub ConvertSelectedMailtoTask() Dim objTask As Outlook.TaskItem Dim objMail As Outlook.MailItem Set objTask = Application.CreateItem(olTaskItem) Set objMail = Application.ActiveExplorer.Selection.Item(1) With objTask .Subject = objMail.Subject .StartDate = objMail.ReceivedTime .Body = objMail.Body 'Add the message as an attachment .Attachments.Add objMail .Save End With Set objTask = Nothing Set objMail = Nothing End Sub
Tools
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. |
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
More Information
Other macros to create tasks or appointments are at
- Automatically create a task when sending a message
- Create an Outlook Appointment from a Message
- Create Task or Appointment and Insert Selected Text
- Create Tasks from Email and move to different Task folders
- How to set a flag to follow up using VBA
- Replicate GTD: Create a task after sending a message
Hi Diane I'm new to VBA but enjoying the cool features we can add like changing the subject line, reply all with attachment and automatically creating a task if someone sends me an email with "can you" [do something] in the body.
The create task wors fine but the maco doesn't seem to add the email as an attachment - for example in Outlook 365 you can drag an email to the to-do and it will maintain the email so when you click on the task it brings up the email.
Any thoughts? thanks!
The last macro should add it as an attachment -
'Add the message as an attachment
.Attachments.Add objMail
Hi - thanks but I think I broke it...code is below - any thoughts would be appreciated!
Sub ConvertMailtoTask(Item As Outlook.MailItem)
Dim objTask As Outlook.TaskItem
Set objTask = Application.CreateItem(olTaskItem)
With objTask
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.DueDate = Item.ReceivedTime + 1
.Importance = olImportanceHigh
.Body = Item.Body
Dim newAttachment As Outlook.Attachments
Set newAttachment = objTask.Attachments
newAttachment.Add Item, olEmbeddeditem
If Item.Attachments.Count > 0 Then
CopyAttachments Item, objTask
.Attachments.Add objMail
End If
.Save
End With
Set objTask = Nothing
End Sub
in the .Subject = item.subject , what if I want to add additional text to the subject for example the word Follow up: Subject
Use
.Subject = "followup: " & item.subject
Hi Diane, I'm wondering if there is a way (or script) that would send an email from the inbox straight to a folder (say '2020') after it has been turned into a task with attachment.
Yes, the macro can move the message. Use
objMail move [folder]
I am interested in setting a script/macro to look at uncategorized tasks in a specific task folder and assign a category to them. Has anyone written code to have a script tick through each task in an a folder and do that?
I don't have a macro that does exactly that, but it wouldn't be too hard to do. I have a macro that touches all item in a folder and you'd just need to check for a category and assign one if missing.
https://www.slipstick.com/developer/code-samples/working-items-folder-selected-items/
Awesome! Would you share it (or did I miss it)?
No, i don't have a macro that does it that is ready to use, but its simple to do. Using the all items/selected folder macro at the link I posted, add this where is says to do whatever/ debug.print .subject
Thank you very much for all your help with this. I am new to this and having two issues.
I would like to add the items to a new folder under My Tasks. It would be a folder under my tasks - similar to "To Do List" / "Tasks", but the folder I would like it to be inserted in would be "Follow Up". I cannot get the email to be added to this.
Also, I would like to new task item to have the original category of the email, as well as an added category when it was added to tasks so the rule does not put duplicates in.
Can you help me with this?
Sub ConvertFUMailtoTask(Item As Outlook.MailItem)
Dim objTask As Outlook.TaskItem
Set SPSFolder = Session.GetDefaultFolder(olFolderTasks).Folders("Follow Up")
Set objTask = SPSFolder.Items.Add(olTaskItem)
objTask.Save
With objTask
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.Body = Item.Body
Dim newAttachment As Outlook.Attachments
Set newAttachment = objTask.Attachments
newAttachment.Add Item, olEmbeddeditem
.DueDate = Item.ReceivedTime + 7
If Item.Attachments.count > 0 Then
CopyAttachments Item, objTask
End If
objTask.Categories = Item.Categories
Item.Categories = "Assigned [Task]"
.Save
End With
Set objTask = Nothing
End Sub
Photo of code attached. I am using Outlook 365.
For reference I am using Office 365.
Do you receive any error messages? It looks like it should work.
Thanks again Diane,
After 10 hours of tinkering I was able to get the macro to create the task in my Sharepoint folder.AWEEEEEEESOME SAUUUUCE.
Cannot Thank you enough. Able to make the Help Desk I've always wanted
What was the secret trick? It may help others facing the same problem.
Hi Diane,
I had a mix up of statements. I had "Set objTask = SPSFolder.Items.Add(olTaskItem)
objTask.save"
Once I changed it too ""Set NewTask = SPSFolder.Items.Add(olTaskItem)
NewTask.save"
Everything started working fine. The only minor issue I have is the Macro settings in Outlook. When I Sign the macro and set to "Notifications for digitally signed macros..." It does not run.
When I switch it back to "Notifications for all macros" it works.
Other than that, it works perfect.
>> Macro settings in Outlook. When I Sign the macro and set to "Notifications for digitally signed macros..." It does not run.
Did you try making a new certificate? Quite a few people had problems after a recent update and had to make a new certificate.
I will try that and let you know. Thanks again. Your site has been very useful.
Hi Diane,
Thank you for sharing your script. I was able to get the auto-tasks going just fine. But trying to save or move the task to a Sharepoint Task that I am using for a help desk.
I cannot seem to get it to save to the folder under "Other Tasks"
>> I cannot seem to get it to save to the folder under "Other Tasks"
You need to add it to the path -
Dim subtaskfolder as folder
Set subtasks = spsfolder.folders("Other Tasks") should do it. (if its a subfolder of the team tasks)
once i corrected some of the statements in the code it cleared everything up. I have a mix of "Set objTask" and "Set NewTask". I used your "GetFolderPath" Function and it all worked fine.
Hello Diane,
Can you tell me how that can be achieved ? Currently I have to manually assign task to team members. I wanted to trigger a mail to my team member and that task should automatically to him/her.
Thank You.
To create a task request to send, use .assign and add a recipient, and .send
With objTask
.Assign
.Recipients.Add "email@address.com"
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.Body = Item.Body
.Send
End With