This macro is the result of a question from a "Getting Things Done" user who wanted a better way to manage tasks on his smartphone.
I use the GTD method and I’ve found that assigning categories doesn’t work for me, one reason is I like to sync the tasks to my iPhone. When you just assign categories and dump everything in one big bucket you have to constantly expand and collapse your views to isolate the right group – lots of clicking and mouse moving. I want to select a message from my inbox – then click on a macro button and have it copied to the appropriate task folder – Do It Today, Deferred, etc. It would look like just like the buttons in the Quick Steps, but unfortunately they just assign the category. I found Create a Task from an Email using a Rule but need help making it work.
Copy the following code block, one copy for each Task folder, and replace the Sub name and folder name (in tFolder) with the name of the folder it will apply to.
Sub TasksFoldername() tFolder = "Old Tasks" CreateTasks End Sub
Create QAT or ribbon buttons for the folder macros. To do this, go to File, Options, Customize Ribbon (or Quick Access Toolbar). In Choose commands from, select Macros. Select the folder macros and click the Add button to add them to the QAT or a ribbon. (If using the ribbon, you need to add a New Group first.)
Tip: if you use the QAT, you can assign keyboard shortcuts to the macros.
Create tasks from email
To use, you need the GetCurrentItem function from Outlook VBA: work with open item or selected item. This will allow the code to work with open or selected messages.
Public tFolder As String
Private Sub CreateTasks()
Dim Ns As Outlook.NameSpace
Dim olTask As Outlook.TaskItem
Dim Item As Outlook.mailItem
Set Ns = Application.GetNamespace("MAPI")
' Get Function athttp://slipstick.me/e8mio
Set Item = GetCurrentItem()
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
Set olTask = taskFolder.Items.Add(olTaskItem)
With olTask
.Subject = Item.Subject
.Attachments.Add Item
.Body = Item.Body
.DueDate = Now + 1
.Save
.Display 'show the task to add notes
End With
Set Ns = Nothing
End Sub
' create one macro for Tasks each folder
' add to ribbon or QAT button
Sub TasksFoldername1()
tFolder = "My Tasks"
CreateTasks
End Sub
Sub TasksFoldername2()
tFolder = "Old Tasks"
CreateTasks
End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look 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.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
The macros on this page should be placed in a module.
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
Michael says
Hi Diane
I have the following which is working fine:
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
However I would like it to save the tasks to the task folder of another exachange account that is shared. the other folder is also named "Tasks" (Being the default Task folder for that account.
How do I tell it to save the tasks to the other default task folder?
Thanks in advance
Michael
Diane Poremsky says
Use this to add to other folders -
Set objTask = olTaskFolder.Items.Add(olTaskItem)
to identify the folder olTaskFolder represents, see https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/#shared for the code to identify a shared exchange folder. If the account is open in the profile (as an account), you can use the GetFolderPath function on that page.
Dan P says
Hello Diane,
I am using Outlook 2013. I have attempted to implement the above code but for some reason every time I run it I get the comment "The attempted operation failed. An object could not be found."
Running the debugger shows the following code line highlighted:
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
Could you please help me understand how to fix this? I cannot figure it out. I have ensured all my macro settings are appropriate for testing.
Cheers
Diane Poremsky says
This line: Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder) at a subfolder under your default tasks folder. The actual folder name is set in the variable tFolder, using one of these macros. If you don't have folders under the task folder with those names and want to use the default task folder, use Set taskFolder = Ns.GetDefaultFolder(olFolderTasks)
' create one macro for Tasks each folder
' add to ribbon or QAT button
Sub TasksFoldername1()
tFolder = "My Tasks"
CreateTasks
End Sub
Sub TasksFoldername2()
tFolder = "Old Tasks"
CreateTasks
End Sub
John Z says
Hello Diane,
Thank you for the truly excellent post.
I would like to modify the approach you have on saving the item as an attachment and do the following.
Copy the item body into the task body ( you already do this)
Copy the item attachments to the task as task attachments. ( you attach the original email as an attachment.
My goal is to make the task a mirror copy of the original email (including attachment) without having to open the Item to find the original attachments. Can it be done?
David Bell says
Is there someone I can pay to help me with this? I enjoy the topic but don't have time to learn it.
Diane Poremsky says
I offer training and assistance for $100/hour. You just wanted the attachments added to the task or did you need other things too?
Nikhil says
Dear Diane,
Thanks once again for all the help. May be I am getting too creative, but is it possible that I can create two simulatenous tasks at the same time from the same email? I would like one task to be created in the 'Projects' task folder and another in the 'Next Action' folder - both originating from the same email.
Thanks in advance.
Nikhil
Diane Poremsky says
Sure, just repeat these lines, replacing tfolder with the actual folder name
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
Set olTask = taskFolder.Items.Add(olTaskItem)
With olTask
.Subject = Item.Subject
.Attachments.Add Item
.Body = Item.Body
.DueDate = Now + 1
.Save
.Display 'show the task to add notes
End With
Nikhil says
Diane,
Hope you are doing great. I wanted a few minor tweaks to the code. Please let me know if it is possible.
1.) Can I choose Due Date to be a specific day for e.g. this week Friday or next week Monday?
2.) I am struggling to add the option where the task once created, gets auto-emailed to my Evernote account. You had shared the below link, but it seems to be for a new email. I am looking to forward the task i am creating.
https://www.slipstick.com/developer/create-a-new-message-using-vba/
Diane Poremsky says
1. Yes, you just need to calculate the date.
use this as the date -
.DueDate = Format(dteFriday(Now()), "mm/dd/yy")
and this function - change the formulas for Monday.
Public Function dteFriday(dte As Date) As Date
Dim x As Integer
x = Weekday(dte)
Debug.Print x
If x = 7 Then
dteFriday = dte - 1 + x
Else
dteFriday = dte + 6 - x
End If
End Function
2. do you want to send it as an attachment or as text in the body? this will send it as an attachment and with notes in the body. Add it after the End with that creates the task.
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.To = "Alias@domain.com"
.Subject = olTask.Subject
.Body = olTask.Subject & vbCrLf & olTask.DueDate
.Attachments.Add olTask
.Display
End With
Nikhil says
Dear Diane,
I tried using the code to choose Friday as the default date. I copied the public function too. I am getting a syntax error. Could you please see if I am missing something?
Nikhil says
Dear Diane,
Thanks a lot for the help and special thanks for bearing with my really basic knowledge. The workflow is working exactly as I wanted. Is there a way I can:
1.) Add a step where the task also gets emailed (to my Evernote or ToodleDo account) while it is being created in one of the customized folders?
2.) I can hide the default 'Task' and 'To-Do' folders, so that only my customized folders remain visible.
Thanks once again. You are amazing.
Nikhil
Nikhil says
Thanks Diane in advance for considering my above query.
Diane Poremsky says
1. Sure. Do you want to send it as a task request or as an email containing task information? See https://www.slipstick.com/developer/create-a-new-message-using-vba/ for sample code that creates a message.
2. You can start in Tasks or only show tasks in the To-Do list but you can't hide them - outlook will unhide them.
See https://www.slipstick.com/outlook/tasks/remove-todo-list-tasks-list-outlook/ to start in tasks
Nikhil says
Thanks Diane.
Truly appreciate your help. The code is working perfectly!
Is there a modification possible where the task saves the email as an attachment and doesn't copy the email text? I am seeing it is doing both. I can see the email text and email as an attachment.
Also, if the customized folder isn't a sub-folder within the default Tasks folder, what would be the modification?
Thanks in advance. You are amazing.
Nikhil
Diane Poremsky says
these lines do the body and attachment - delete one or both
.Attachments.Add Item
.Body = Item.Body
this handles the folder -
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
if ts at the same level as tasks, you'd use
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).parent.Folders(tFolder)
Nikhil says
Diane - I will appreciate your help on the above. I know I am asking a lot of basic questions and am truly grateful for your patience.
Nikhil says
Thanks Diane. I am copying the code below I am trying. I have created a sub-folder within Tasks called 'Test1'. I am getting a compile error 'Expected End Sub'. 'Private Sub CreateTasks () is getting yellow highlighted.
Thanks a lot in advance. You are super helpful.
Private Sub CreateTasks()
Dim Ns As Outlook.NameSpace
Dim olTask As Outlook.TaskItem
Dim Item As Outlook.MailItem
Set Ns = Application.GetNamespace("MAPI")
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function
Set Item = GetCurrentItem()
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
Set olTask = taskFolder.Items.Add(olTaskItem)
With olTask
.Subject = Item.Subject
.Attachments.Add Item
.Body = Item.Body
.DueDate = Now + 1
.Save
.Display 'show the task to add notes
End With
Set Ns = Nothing
End Sub
' create one macro for Tasks each folder
' add to ribbon or QAT button
Sub TasksFoldername1()
tFolder = "Test1"
CreateTasks
End Sub
Diane Poremsky says
This part goes at the end of the macros or in a different module (other macros can use it too).
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function
Nikhil says
Thanks Diane,
You are super helpful. I had a few queries. I know I am asking really basic ones, so please bear with me.
1.) I am pasting the code found on this page, as below. There is a text referring to "http://slipstick.me/e8mio". Should I copy and paste the code from that page?
2.) My non-default task folders are "Compass" and "Projects". Should I enter it as per below?
Sub TasksFoldername1()
tFolder = "Compass"
CreateTasks
End Sub
The code which i am using follows...
----------------------------------------------------------------------------------------------------------------
Public tFolder As String
Private Sub CreateTasks()
Dim Ns As Outlook.NameSpace
Dim olTask As Outlook.TaskItem
Dim Item As Outlook.mailItem
Set Ns = Application.GetNamespace("MAPI")
' Get Function at http://slipstick.me/e8mio
Set Item = GetCurrentItem()
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
Set olTask = taskFolder.Items.Add(olTaskItem)
With olTask
.Subject = Item.Subject
.Attachments.Add Item
.Body = Item.Body
.DueDate = Now + 1
.Save
.Display 'show the task to add notes
End With
Set Ns = Nothing
End Sub
' create one macro for Tasks each folder
' add to ribbon or QAT button
Sub TasksFoldername1()
tFolder = "My Tasks"
CreateTasks
End Sub
Sub TasksFoldername2()
tFolder = "Old Tasks"
CreateTasks
End Sub
Diane Poremsky says
1. Yes, you need the getcurrentitem function so the macro works with open or selected items.
2. Yes. Do it like that. You'll have one for Compass and one for Projects. The code assumed they are subfolders of tasks - if not, you'll need to make a minor edit to the code.
Nikhil says
Dear Diane,
This is a super useful functionality. My challenge is that I am not as adept at VBA scripting as most are. Would it be possible for me to have a step-by-step instructions of what should I do? I will be eternally grateful.
Thanks in advance.
Diane Poremsky says
see https://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/ - it has the instructions to use VBA. Copy the code on this page to a new module (right click on Project1 and choose insert > module) then customize the ribbon or quick access toolbar to add buttons for the short that tell the code which folder to use.
Michael Muselík (@MichaelMuselik) says
Hi Diane, thats almost exactly, what I need. But ... there is any possibility to modify this macro to force select task folder in pop-up window? I usually use three task folders. Thnx a lot.
Diane Poremsky says
If you don't want to use 3 separate buttons (one for each task folder) you could use a userform or a folder picker.
The basics of a userform as a picker is here. I'll have to see if i can find the Outlook folder picker macro - it's here somewhere. :)
Diane Poremsky says
Ah.. here is the folder picker code snippet
Dim objApp As Application
Dim objNS As NameSpace
Dim objFolder As MAPIFolder
Set objApp = CreateObject(“Outlook.Application”)
Set objNS = objApp.GetNamespace(“MAPI”)
Set objFolder = objNS.PickFolder
mike says
thanks for the cool utility. it creates the task in the right folder and then displays it. the only problem im having is that the changes i make when i edit the subject or note field on the newly created task dont actually save for some reason. any ideas?
Diane Poremsky says
so clicking Save after making the changes doesn't work? Do you get any error messages?
mike says
no error message. i change the subject a little and add some text in the body and click save and close. no error. but when i look go into the specific task folder the changes didnt take.
Diane Poremsky says
I'll try to repro - it *should* work.
mike says
thanks Diane. let me know if you need anymore info.
mike says
hi diane, i see what is happening....when i make changes to subject after the task is displayed and then save/close it actually saves a new task in the main Task folder versus saving the info to the task created in the specified folder....any ideas?
Diane Poremsky says
What type of email account? It might be a problem with the account type - I'll look into it.
Mike says
Outlook 2013 professional. Exchange server. the interesting thing is that at the bottom of the task window of the task that is displayed from the vba script it says "in shared folder Tasks".... however when I manually open the task that was created and moved into the correct folder it correctly displays "in shared folder @errands"... its like it forgot what folder it was in when the .display code executes...
Diane Poremsky says
So its moved but the folder name is not updating in the status bar?
Drew says
Super excited about this! But I also am seeing the same thing Mike is seeing. If I make a change after .Display and save the task, I end up with a task in the right folder but with none of my changes applied, and a task in the default Task folder with the changes applied.
Diane Poremsky says
Ok - i figured it out - instead of using the default folder and moving the task, set the correct folder to add the task to it.
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
Set olTask = taskFolder.Items.Add(olTaskItem)
With olTask
.Subject = Item.Subject
.Attachments.Add Item
.Body = Item.Body
.DueDate = Now + 1
.Save
.Display 'show the task to add notes
End With
Mike says
The task was created and moved with the email data into the sub folder as expected. However when the vba code displayed the task for editing it appears to think it's in the default task folder per the status bar at the bottom of the window. If I make a change and save it saves a new task into the default task folder. If I go into the sub folder and manually click open the task it works fine and the status bar shows it in the sub folder. One would think the display command would work just like manually clicking open the task in the sub folder but something is confusing it
Diane Poremsky says
A lot of the VBA commands don't work exactly like the commands in the ribbon. You're working with the original, now unsaved, task - when you saved/moved it, it moved the saved copy not the onscreen copy.
FYI - i updated the macro to use a specific task folder instead of moving the task. This should fix the problem.
mike says
works great!! you are awesome Diane. thanks!
brian davis says
Outlook 2013. and actually, all i need is to move the task to a specific folder. the task is already created. What i want is a button on the ribbon that will save the task to a specific folder.
Diane Poremsky says
Is the task open? If so, this will move the task to a subfolder of the default tasks folder:
Sub MoveTask()
Dim Ns As Outlook.NameSpace
Dim oApp As Outlook.Application
Dim oTask As Outlook.TaskItem
Dim moveTo As Outlook.folder
Set Ns = Application.GetNamespace("MAPI")
Set oApp = Outlook.Application
Set oTask = oApp.ActiveInspector.CurrentItem
Set moveTo = Ns.GetDefaultFolder(olFolderTasks).Folders("My Tasks")
oTask.Move moveTo
End Sub
If it's not open, set the task using this instead
Set oTask= oApp.ActiveExplorer.Selection.Item(1)
https://www.slipstick.com/developer/outlook-vba-work-with-open-item-or-select-item/
brian davis says
This isn't working. i get a "type mismatch" error when i run this.
Diane Poremsky says
Which version of Outlook? does it highlight any specific line in yellow when the error comes up? Oh, and are you running it on an email message? If you need to run it on other items that arrive by email, like NDR or reports, change this Dim Item As Outlook.mailItem to Dim Item As Object. And get the GetCurrentItem function from http://slipstick.me/e8mio