Question posted 2011 · +3 upvotes
I have a list of data, let’s say client information (Name, Email, Amount Owing etc.), stored in an Excel worksheet . My aim is to click a button in Excel and send each client their information in an Outlook Template.
- create a mail object
- set the mail object to the template file
- setting and then filling in the template with data about the current client – mostly stuck here, not sure how to specify variables in a template and then relate to them in VBA
- save to drafts for later review/send
eg. Dear << clientname >> = Dear John Smith
My code thus far:
Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:egTemplate.oft")
With MyItem
.To = Worksheets("Clients").Range(1, 2)
.Subject = "Monthly bill"
'Refer to and fill in variable items in template
.Save
End With
Set MyItem = Nothing
Set MyOlApp = Nothing
I need to know if this is even possible, can I do it without plugins and if so, does anyone know a good link/tutorial I can follow?
Thanks in advance and kind regards
Update Added my code and made some changes to the request
Accepted answer +8 upvotes
Here is what you can do :
With MyItem
'Refer to and fill in variable items in template
.Body = Replace(.Body, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
or, if your mail is in HTML:
With MyItem
'Refer to and fill in variable items in template
.HTMLBody = Replace(.HTMLBody, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
Tested successfully on Excel / Outlook 2007
2 code variants in this answer
- Variant 1 — 4 lines, starts with
With MyItem - Variant 2 — 4 lines, starts with
With MyItem
Excel VBA objects referenced (4)
Application— Using events with the Application objectApplication— Working with Other ApplicationsRange— Refer to Cells by Using a Range ObjectRange— Delete Duplicate Entries in a Range
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.