# Create a Task from an Email using a Rule

Last reviewed on November 22, 2014   —  157 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)
.Subject = Item.Subject
.Body = Item.Body
.Save
End With
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.




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)





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



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
End If


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
.Subject = Item.Subject
.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

.Assign
.Subject = Item.Subject
.Body = Item.Body
End With

myDelegate.Resolve



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.

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


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

' do whatever
'

' After you save the task, move it to the other folder



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

Sub ConvertSelectedMailtoTask()
Dim objMail As Outlook.MailItem

For Each objMail In Application.ActiveExplorer.Selection

.Subject = objMail.Subject
.Body = objMail.Body
.Save
End With

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

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

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

### 157 responses to “Create a Task from an Email using a Rule”

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

1. I'm not 100% sure what you are asking - if you mean in the view when you open the contacts folder, it's either the view settings (for address card and other views) or the business card settings.

On the address card and other views, its as simple as changing the view. For business cards, you need to change every card. If you want this as default, see http://www.slipstick.com/outlook/contacts/change-.../ and http://www.slipstick.com/outlook/using-custom-bus...

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

1. Of course it's possible. The code needs just a little tweaking -
Dim objMail As Outlook.MailItem

For Each objMail In Application.ActiveExplorer.Selection

Next
Set objMail = Nothing
End Sub

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

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

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

1. Tasks have flags by default. You might want due date & reminders -
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. 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.

1. 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. Thanks for the very useful code - You are truly Most Valuable!

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

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

9. 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#

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

10. OK and i asked and they said they would make it a product suggestion

Thanks

11. This works awesome and so grateful I was able to stumble across this tip.
Would you by chance know how to save the task into the sharepoint list instead of the regular list within Outlook?

1. You need to use getfolderpath

Add the two bolded lines to the code and change the name of the tasks folder
-- snipped code --

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

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

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

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

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

1. 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
End If

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

16. So just one "if Item.Attachment.Count..." statement, right?

1. Yes. You only need 1 statement and it will loop to get all attachments.

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

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

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

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

19. Me sirvió mucho, gracias

20. Bump!
:)
Hi Diana - any chance you have advice on how to setup an auto-rule to a sahred task folder?

1. What exactly do you need to do? It's hard to impossible to run rules on shared folders.

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

1. Yes, but you need to parse the text to locate the date. This is one method: http://www.slipstick.com/outlook-developer/parsing-text-fields-in-outlook/

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

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

2. 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
.Subject = Right(Item.Subject, Len(Item.Subject) - 6)
.Body = Item.Body
.Save
End With
Item.Save
End If
End Sub

23. Thanks very much! This is great.

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

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

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

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

1. You need to change this line -
Set objTask = GetFolderPath("New PSTTest Cal")

and use the function at use non-default folders

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

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

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

1. If you are using the VBA, Remove the .Body = Item.Body line and add
Dim newAttachment As Outlook.Attachment

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

1. You mean this one? Create a task when a message is flagged You put this in ThisOutlookSession with the other code.

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

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

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

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

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

1. 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
If Weekday(dayDue) = vbSaturday Then
dayDue = dayDue + 2
Else
If Weekday(dayDue) = vbSunday Then
dayDue = dayDue + 1
End If
End If

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

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

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

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

1. Check the Macro security. This usually means security was set back to not allow.

36. Thank you. That was the problem. Works fine again now.

37. I would like to create 2 tasks for certain emails how would I go about doing this?

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

.Subject = "Task 1" & item.Subject
.Body = item.Body
.Save
End With

38. Hi Diane,

I would like to auto assign this task to another team member, how would i do this?

Thanks

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

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

1. That setting is the problem. you can change it to the lowest level or use Seifert to sign it. Instructions are at http://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/

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

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

THE CODE:

Dim tarih As Date
Dim mesajicerik As String
Dim tarihyeri As Integer
.Subject = Item.Subject
.Body = Item.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
End Sub

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

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

Dim tarih As Date
Dim mesajicerik As String
Dim tarihyeri As Integer
.Subject = Item.Subject
.Body = Item.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
End Sub

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

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

Dim tarih As Date
Dim mesajicerik As String
Dim tarihyeri As Integer
.Subject = Item.Subject
.Body = Item.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
End Sub

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

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

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

Dim mesajicerik As String
Dim tarihyeri As Integer
.Subject = Item.Subject
.Body = Item.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
End Sub

1. You also need to set it. Try .ReminderSet = True

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

Needs to be changed to

45. Thank you, and if I've saved the custom form in the personal forms library, what should I do?

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

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

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

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

48. Here is what I have in VB:

It Works!
.Subject = Item.Subject
.Body = Blank
Dim newAttachment As Outlook.Attachment
(Item, Outlook.OlAttachmentType.olEmbeddeditem)
.Save
End With
End Sub

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

-- snipped code --

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

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

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

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

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

52. 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!)

53. Is it possible to create an appointment in the calender with this script, instead of a task?

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

54. 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.ReminderSet = True
objAppt.Mileage = 30
objAppt.Save

Set objAppt = Nothing
Set objMail = Nothing

End Sub

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

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

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

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

1. It is possible. See http://www.slipstick.com/developer/code-samples/create-meetings-csv-file/ for the method needs to get values from an excel sheet.

58. 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
fso.DeleteFile strFile
Next

Set fldTemp = Nothing
Set fso = Nothing
End Sub
Dim objMail As Outlook.MailItem

For Each objMail In Application.ActiveExplorer.Selection

.Subject = objMail.Subject
.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
End If
.Save
End With

Next
Set objMail = Nothing
End Sub

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

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

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

If it's a published form, try the published form name "ipm.task.mine", if that doesn't work , use item.add:

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

3. Oops. You need to tell it which folder to use.
Dim oFolder As Outlook.Folder

4. That's it! Thanks so much.

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

6. Enter the date as #1/1/4501# to get a value of none.

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

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

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

.Subject = Item.Subject
.Body = Item.Body
.ReminderSet = True
.Save
End With
.Subject = "Task 1" & objMail.Subject
.Body = Item.Body
.ReminderSet = True
.Save
End With

End Sub

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

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

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

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

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

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

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

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

64. Hi,

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

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

Set objMsg = Application.ActiveExplorer.Selection(1)
objTask.Subject = objMsg.Subject & " (from: " & objMsg.Sender & ")"

Set objMsg = 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

1. You need to use .Links.Add objContact - you'll need to create the contact it if doesn't exist or resolve it if it does.

You may need to show the contact linking field too - I know in outlook 2013, you need to set the showcontactfieldobsolete key.

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

3. 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'

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

then use

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

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

And then:

Or, even better:

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

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

'=====================
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)
If TypeName(objContact) = "Nothing" Then

Set objContact = Application.CreateItem(olContactItem)

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

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

.Display
.Categories = objMsg.Categories
.Subject = objMsg.Subject & " (from: " & objMsg.Sender & ")"
End With

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

Dim objMail As Outlook.MailItem

For Each objMail In Application.ActiveExplorer.Selection

objTask.ReminderTime = objTask.DueDate - 0.25 ' remind at 6 AM 1 days from now
If Item.Attachments.Count > 0 Then
End If

Next
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
fso.DeleteFile strFile
Next

Set fldTemp = Nothing
Set fso = Nothing
End Sub

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

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

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

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

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

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

1. Do you want it to automatically create the task? You'll need to use an itemadd macro and watch the sent items folder. You could also use an itemsend macro to create a task after you hit Send but before the item is sent.

A sample itemsend macro is here - http://www.slipstick.com/developer/code-samples/set-flag-follow-up-using-vba/ - replace the flag close with the new task code.

2. 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
.Subject = Item.Subject
.Body = Item.Body
.Categories = "SI's"
.StartDate = Date
.DueDate = Date + ResponseDays
.ReminderTime = Date + ResponseDays
.ReminderSet = True
.Save
End With
End If
End If
End Sub

3. 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:

I've also tried to use:

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

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.

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

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

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

1. 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.
.Assign
.Subject = Item.Subject
.Body = Item.Body
'.Send
.Display
End With

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

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

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

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

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

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

4. how would I configure that automatic email to be sent out using a macro & rules?

5. You could change this to send a reply - http://www.slipstick.com/developer/code-samples/change-category-task-marked-completed/ - you'll need to put the email address in the task, either in the body or in a new user-defined field.
Code sample to generate an email is here - http://www.slipstick.com/developer/create-a-new-message-using-vba/

6. I threw those code samples together to create the sample in this file: send task update

7. 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()
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
Dim objProp As Outlook.UserProperty

Set objProp = .UserProperties.Add("alexis@century21city.com", olText, True)
.Subject = Item.Subject
.Body = Item.Body
.Save
End With
End Sub

8. 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
.Display ' use .send to automatically send it
End With

and this to create the task - it should be what you used before:

.Subject = Item.Subject