If you want to set certain Outlook fields when you do a mail merge, you need to use an ItemSend rule.
To use, open the VBA Editor using Alt+F11 and expand Project1 until you see ThisOutlookSession. Double click on ThisOutlookSession and paste the macro in the right pane.
Updated July 26 2017: Changed the macro to use a codeword in the subject and remove the codeword before sending.
This macro is not limited to mail merges; it checks the subject of every message you send to see if it meets the condition of the If statement.
To avoid running the macro on all messages you send, I'm using a codeword in the subject. If the word is found, Outlook removes it from the subject and runs the macro on the messages, adding attachments and setting other fields as needed.
If you want to change the codeword, choose something unique that you would not expect to use in an email. Remember: the codeword is case-sensitive. What you use in the macro is also what you'll use in the subject field.
If you are setting a reminder, you'll want change the reminder date and time. You can set the reminder to a specific date and time using #10/30/2012 8:00:00 AM# format or use Now + 1 for this time tomorrow (or any number of days in the future). If you use the deferred delivery option, you can use SendAt + 1 to set a reminder for 24 hours after the deferred send time.
Recipients may have rules to remove the reminder, flag, and Importance fields.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) If InStr(LCase(Item.Subject), "[merge]") Then 'Defer delivery time SendAt = DateSerial(Year(Now), Month(Now), Day(Now) + 1) + #7:00:00 AM# With Item .Subject = Replace(.Subject, "[merge]", "") .Importance = olImportanceHigh .VotingOptions = "I accept;I decline;I don't care" .ReminderSet = True .ReminderTime = SendAt + 1 '#10/30/2012 8:00:00 AM# .FlagRequest = "Please reply by " & .ReminderTime .DeferredDeliveryTime = SendAt ' to add an attachment, enter the path & name here .Attachments.Add "C:\Users\username\Documents\TEST.xlsx" End With End If End Sub
If you want to use a different attachment for each person, you'll need to use attachments that are the same name as a message field - the To field will probably be the easiest, although prone to problems. ("To" is the display name, not the email address.)
Item.Attachments.Add "D:\For merge\" & Item.To & ".docx"
Or use one of the utilities listed at Using Mail Merge in Outlook
I am not having any luck at the moment. I've tried restarting outlook, but I'm still just receiving messages that say [merge] in the subject with no attachments. I've enabled all macros and can't seem to get any traction.
Here's the version of the code I'm using.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If InStr(LCase(Item.Subject), "[merge]") Then
'Defer delivery time
SendAt = Now + TimeSerial(0, 2, 0)
With Item
.Subject = Replace(.Subject, "[merge]", "")
.Importance = olImportanceHigh
.DeferredDeliveryTime = SendAt
' to add an attachment, enter the path & name here
.Attachments.Add "Z:\"FILENAME".pdf"
End With
End If
End Sub
Hi Diane,
Thank you for your great code.
I have tried in Outlook 2013 and its work but not when I tried in Outlook 2019. Whats setting should I change?
i did everything but still nothing happen, no attachment and the email keep being sent.
is this still working until today?
Hi Diane - Thank you for this - ive tried adding SentOnBehalfOfName = "user@domain.com" but it doesnt work. Any suggestions please?
Could someone assist me with the following so that it will send individual attachments as mentioned above;
Where do I include this...
Item.Attachments.Add "D:\For merge\" & Item.To & ".pdf"
Also, is there a way to use an email vs the display name?
Thank you
Quick help for anyone encountering the same problem:
The code word (["merge"]) needs to be written in small letters within the MS visual basic. Otherwise it will not work.
Thank you very much for this.
You saved me spending £30 on a plugin to do the same!
This is so great.. and can we attach more than 1 file?
and how to edit this command, if we want to send it immediately?
Many thanks.
Remove the line to send immediately.
Repeat this line for each attachment - changing the path of course. :0
.Attachments.Add "C:\Users\username\Documents\TEST.xlsx"