Forum

Excel VBA to change...
 
Notifications
Clear all

Excel VBA to change placeholders in outlook message template like mail merge

2 Posts
2 Users
0 Reactions
156 Views
(@soujanyabargavi)
Posts: 1
New Member
Topic starter
 

Can you help me with why this doesn't work? As of right now, this code will open up an email template that I created but I would like it to find the %recipient% on the opened email and replace it from the excel sheet with the recipient name in range B2. 

I have just pieced code that I have found online together to make it this far but could use some help as to why excel doesn't recognize and change the%recipient% on the outlook message template I created. The attempt is to make it like the mail merge function that is used between excel and word but with excel and an outlook email template.

Sheets("Email Addresses").Range("A2").Select Do Until IsEmpty(ActiveCell.Value) ActiveCell.Offset(1, 0).Select 'Looping Dim OutApp As Object Dim OutMail As Object Dim count As Integer EmailTo = Worksheets("Email Addresses").Range("A2") Recipient = Worksheets("Email Addresses").Range("B2") Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItemFromTemplate( _ Sheets("Start Here").Range("B25")) On Error Resume Next ' Change the mail address and subject in the macro before you run it. With OutMail .To = EmailTo .Body = Replace(.Body, "%recipient%", Recipient) .Display
 
Posted : 14/07/2018 2:17 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi,

Without your files to look at it's hard to work out what you code is doing.

If you want to change the email address you are sending the email to you can use

.To = Range("B2").Value

Please refer to this post which may be able to help you 

https://www.myonlinetraininghub.com/error-creating-relationship-in-vba-userform-code-to-create-pdf-from-excel-worksheet-then-email-it-with-outlook

Regards

Phil

 
Posted : 16/07/2018 11:29 pm
Share: