This application startup macro watches for the user to send an email and asks if they want to create a task for the message. If they choose no, the message is sent. If they select yes then a task is created before the message is sent.
The code adds the message recipients to the task body, along with the message body. The start date is "today" (when the message is sent) and the due date is 2 days from now. The reminder is set for 2 days from now at 9 AM.
To automatically create tasks for messages you receive, see Create a Task from an Email using a Rule
Create task from sent message macro
To use this macro:
- Set macro security to low.
- Open the VBA Editor (Alt+F11)
- Expand Project1 to find ThisOutlookSession
- Copy the macro and paste at the top of ThisOutlookSession
Edit the start date, due date, and reminder time fields as needed.
To test the macro, click in the Application_Startup procedure and click the Run button then send a message.
May 14 2018: update the code to fix a problem where the reminder was set for the beginning of this month, if creating the task at the end of the month.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim intRes As Integer Dim strMsg As String Dim objTask As TaskItem Set objTask = Application.CreateItem(olTaskItem) Dim strRecip As String strMsg = "Do you want to create a task for this message?" intRes = MsgBox(strMsg, vbYesNo + vbExclamation, "Create Task") If intRes = vbNo Then Cancel = False Else For Each Recipient In Item.Recipients strRecip = strRecip & vbCrLf & Recipient.Address Next Recipient With objTask .Body = strRecip & vbCrLf & Item.Body .Subject = Item.Subject .StartDate = Item.ReceivedTime ' Can use Now + nn for the start and/or due dates ' .DueDate = Now + 10 ' .StartDate = Now + 9 .ReminderSet = True .ReminderTime = Now + 2 + #9:00:00 AM# ' alternately, use the due date to set the reminder: ' .ReminderTime = .DueDate - 2 + #2:00:00 PM# .Save End With Cancel = False End If Set objTask = Nothing End Sub
Add Sent Message as attachment
If you want to add the sent message as an attachment, you can't use .Attachments.add because it adds a blank message. One way to do it is to watch the Sent folder for a new item and attach it to the Task.
While you could use the Itemsend macro then add the attachment after the message is moved to the sent folder, this version of the macro watches the sent folder and asks if you want to create at task after the message is sent.
You'll need to restart Outlook or click in the Application_Startup macro then click Run to start the macro.
Private objNS As Outlook.NameSpace Private WithEvents objItems As Outlook.Items Private Sub Application_Startup() Dim objWatchFolder As Outlook.Folder Set objNS = Application.GetNamespace("MAPI") 'Set the folder and items to watch: Set objWatchFolder = objNS.GetDefaultFolder(olFolderSentMail) Set objItems = objWatchFolder.Items Set objWatchFolder = Nothing End Sub Private Sub objItems_ItemAdd(ByVal Item As Object) Dim sentMsg As Object Dim objTask As TaskItem Dim intRes As Integer Dim strMsg As String Set objTask = Application.CreateItem(olTaskItem) Dim strRecip As String strMsg = "Do you want to create a task for this message?" intRes = MsgBox(strMsg, vbYesNo + vbExclamation, "Create Task") If intRes = vbNo Then Cancel = False Else For Each Recipient In Item.Recipients strRecip = strRecip & vbCrLf & Recipient.Address Next Recipient Debug.Print Item.Subject With objTask .Body = strRecip & vbCrLf & Item.Body .Subject = Item.Subject .StartDate = Item.ReceivedTime ' Can use Now + nn for the start and/or due dates ' .DueDate = Now + 10 ' .StartDate = Now + 9 .ReminderSet = True .ReminderTime = Now + 2 + #9:00:00 AM# ' alternately, use the due date to set the reminder: ' .ReminderTime = .DueDate - 2 + #2:00:00 PM# .Attachments.Add Item .Save End With End If Set Item = Nothing End Sub
Add the Recipient Display name to the task
This version of the code adds the recipients name to the task. You can use the full name or the first word in their name (hopefully it's the first name)
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim intRes As Integer Dim strMsg As String Dim objTask As TaskItem Set objTask = Application.CreateItem(olTaskItem) Dim strRecip As String strMsg = "Do you want to create a task for this message?" intRes = MsgBox(strMsg, vbYesNo + vbExclamation, "Create Task") If intRes = vbNo Then Cancel = False Else ' get the name, not the email address For Each Recipient In Item.Recipients strRecip = strRecip & vbCrLf & Recipient.Name Next Recipient ' get the first name in the display name StrSplit = Split(strRecip, " ") With objTask .Body = strRecip & vbCrLf & Item.Body .Subject = StrSplit(0) & ": " & Item.Subject .StartDate = Item.ReceivedTime ' Can use Now + nn for the start and/or due dates ' .DueDate = Now + 10 ' .StartDate = Now + 9 .ReminderSet = True .ReminderTime = Date + 2 + #9:00:00 AM# ' alternately, use the due date to set the reminder: ' .ReminderTime = .DueDate - 2 + #2:00:00 PM# .Save End With Cancel = False End If Set objTask = Nothing End Sub
Video Tutorial
This tutorial shows how to add the macro to Outlook and use it.
How to use the Macro
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. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.
To use the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
Application_Startup macros run when Outlook starts. If you are using an Application_Startup macro you can test the macro without restarting Outlook by clicking in the first line of the Application_Startup macro then clicking the Run button on the toolbar or pressing F8.
More information as well as screenshots are at How to use the VBA Editor.
Hi Diane,
strMsg = strMsg & "Please find the pay slip of April 2023 attached." & vbcrlf & vbcrlf
I'm using the the above statement in the vbscript (notepad) for sending bulk mailers....kindly let me know how to choose font color
Hello Diane:
This macro has been working great, however, recently the macro has stopped after this step:
'removes your signature from the top of the Forward
If objDoc.Bookmarks.Exists("_MailAutoSig") Then
Set oBookmark = objDoc.Bookmarks("_MailAutoSig")
oBookmark.Select
objDoc.Windows(1).Selection.Delete
End If
Any ideas on what it could be?
Any error messages? Does it work if you remove that line?
If the bookmark doesn't exist, it should end, not error, so it shouldn't be that line causing the error.
Hello Diane - is there a way to NOT have this run when responding to a meeting (accepting, tentative, decline). I love this macro and however just wondering if there is a work around for meetings.
Yes, use an if statement as the first line -
if instr(1, item.messageclass, "IPM.Meeting") > 0 then exit sub
Diana - worked like a charm!!! Thanks so much.
Hi,
I'm trying to use the Add Sent Message as Attachment macro however i'm getting the below error for the second line of the code:
Compile error:
Invalid attribute in Sub or Function
Private WithEvents objItems As Outlook.Items
I'm currently using Outlook 2013. Are you able to help correct the VBA?
I am also using your code here in order to create tasks from an email and automatically assign the task to a specified task folder. Is there a way to combine the two? I'm hoping to get two prompts whenever an email is sent: first prompt asks if I want to create a task (this is accomplished via your code above), and the 2nd prompt asks the user to select the desired task folder (for me, the folders are 'Action', 'Waiting', and 'Someday').
Yes, you can do that.
the prompt would get the tFolder variable
Set taskFolder = Ns.GetDefaultFolder(olFolderTasks).Folders(tFolder)
Just correcting myself: The information I want to add should go to the subject of the task, not of the sent message. I'd like to see tasks like:
Recipient.Firstname - Subject of my sent e-mail MM/DD
David - Finalize Proposal on XYZ 02/09
Then I would have a nice list to talk about all things I have to talk about with David :-)
Getting just the first name is tough - you can easily get the display name and could split it at the first space, keeping the first segment, but it wont necessarily be the first name.
To get the first name, you could do a look up of the contact and take it from the contact. I have some code on the site that does a contact lookup for new messages (to add categories to the message) so its just a matter of putting the lookup code into this code.
This will get the first word in the display name:
For Each Recipient In Item.Recipients
strRecip = strRecip & vbCrLf & Recipient.Name
Next Recipient
StrSplit = Split(strRecip, " ")
With objTask
.Body = strRecip & vbCrLf & Item.Body
.Subject = StrSplit(0) & " " & Item.Subject
Hi Diane,
thank you for providing this handy macro. I used the version with the date-added subject, and I'd like to add the recipient's name to the subject string, if possible, first name only. Adding "& Recipient.Name" to the subject line seems not to be sufficient, and that's already the end of my VBA knowledge. Could you help me out here?
Cheers,
Franz-Josef
Hi Diane,
I think there might be another comment pending for your revierw. I promise this is the last request!
Cheers,
David
>> I promise this is the last request!
I'll hold you to that. LOL
(Yes, I'm way behind in answering comments.)