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:

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

starl

Administrator
Joined
Aug 16, 2002
Messages
5,969
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
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?
 

jamesuk

Board Regular
Joined
Sep 8, 2015
Messages
85
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:

jamesuk

Board Regular
Joined
Sep 8, 2015
Messages
85
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 :)
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,512
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.
 

starl

Administrator
Joined
Aug 16, 2002
Messages
5,969
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
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)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,564
Messages
5,596,875
Members
414,106
Latest member
Tigretto

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
Top