Replace Fields in Outlook Mail

jamesuk

Board Regular
Joined
Sep 8, 2015
Messages
85
Hi All

I found this bit of code that appears to exactly what I need, but I can't get this one bit to work for me. It should be replacing "<< clientname >>" with the value in range A15, but it's not doing it?

Can anyone see a reason as to why it would not be working for me?

Many thanks for your help in advance! :)

Code:
[FONT=Verdana]Sub testing()[/FONT]



[FONT=Verdana]Dim myOlApp As Outlook.Application[/FONT]
[FONT=Verdana]Dim MyItem As Outlook.MailItem[/FONT]

[FONT=Verdana]Set myOlApp = CreateObject("Outlook.Application")[/FONT]
[FONT=Verdana]Set MyItem = myOlApp.CreateItemFromTemplate("\\DATA1\myname$\Desktop\Projects\M\mytemplate.oft")[/FONT]

[FONT=Verdana]With MyItem[/FONT]
[FONT=Verdana]    .To = Worksheets("lookup").Range("A14")[/FONT]
[FONT=Verdana]    .Subject = "Monthly bill"[/FONT]
[FONT=Verdana]    'Refer to and fill in variable items in template[/FONT]
[FONT=Verdana]    .HTMLBody = Replace(.HTMLBody, "<< Clientname >>", Sheets("Lookup").Range("A15"))[/FONT]
[FONT=Verdana]    .Display[/FONT]



[FONT=Verdana]End With[/FONT]

[FONT=Verdana]Set MyItem = Nothing[/FONT]
[FONT=Verdana]Set myOlApp = Nothing[/FONT]


[FONT=Verdana]End Sub[/FONT]
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
it's case sensitive. You have shown your field with two different cases - in your text, it's lowercase; in your code, it's initial case. Which is it in your template?
 
Upvote 0
Hi Starl

Thanks for your response

Sorry, my mistake. in the email template it's,
Code:
<< Clientname >>

so both start with upper case

Regards
 
Last edited:
Upvote 0
I managed to figure it out! :)

If I take away the
Code:
<< >>
and just leave the word, it works fine!

Thank you for your assistance though :)
 
Upvote 0
Be aware, too, that << Clientname >> is not the same as <<Clientname>>, « Clientname », or «Clientname». Your Replace code is necessarily very particular about what it replaces.
 
Upvote 0
Hi Paul, I hadn't considered that the brackets may be the wrong ones. Good catch. That's why I prefer using square brackets [] when I do things like that - much easier to find that key on the keyboard (vs memorizing the ALT code)
 
Upvote 0

Forum statistics

Threads
1,214,853
Messages
6,121,935
Members
449,056
Latest member
denissimo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top