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
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
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.
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
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
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?
Is there someone I can pay to help me with this? I enjoy the topic but don't have time to learn it.
I offer training and assistance for $100/hour. You just wanted the attachments added to the task or did you need other things too?
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
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
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/
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
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?
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
Thanks Diane in advance for considering my above query.
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
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
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)
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.
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
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