Replace a placeholder in a VBA code

Paula777

New Member
Joined
Jul 6, 2022
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
Hello all,

I am new to VBA and am trying to create my first code to automate sending mass emails to various email adresses with the same subject and the same body of the email (written and taken from a text box) but I need to adjust and personalize one amount value which I need to be different for each email. In the text box which is the body of the email I put a placeholder XYZ and I want this to be replaced with values from column E (amount column)

My code looks like this:

....


Dim i as Integer
Dim last-row as Integer
Last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))

For i = 2 To last row
Set msg = OA.createitem(0)

Msg.to = sh.Range("B" & i).Value
Msg.subject = sh.Range("C" & i).Value
Msg.body = sh.TextBoxes("TextBox 1").Text

And here i wanted to add sth like in order to replace XYX with the values from the Amount column:

Amount = Range("E" & i).Value
' replace place holders
Body = Replace(body, "XYX", amount)

But it doesnt work. It doesnt show any error, it simply doesnt do anything.

Do you maybe know how to fix it and replace the XYX from the text box which is put as the body of the email for the values from the E Column?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Maybe use qualifiers. Body is not the same as Msg.Body. It is a new undeclared variable. This would have been caught as a compiler error if you used Option Explicit.
Rich (BB code):
Amount = sh.Range("E" & i).Value
' replace place holders
Msg.Body = Replace(Msg.Body, "XYX", amount)

I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.
 
Upvote 0
Hello all,

I am new to VBA and am trying to create my first code to automate sending mass emails to various email adresses with the same subject and the same body of the email (written and taken from a text box) but I need to adjust and personalize one amount value which I need to be different for each email. In the text box which is the body of the email I put a placeholder XYZ and I want this to be replaced with values from column E (amount column)

My code looks like this:

....


Dim i as Integer
Dim last-row as Integer
Last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))

For i = 2 To last row
Set msg = OA.createitem(0)

Msg.to = sh.Range("B" & i).Value
Msg.subject = sh.Range("C" & i).Value
Msg.body = sh.TextBoxes("TextBox 1").Text

And here i wanted to add sth like in order to replace XYX with the values from the Amount column:

Amount = Range("E" & i).Value
' replace place holders
Body = Replace(body, "XYX", amount)

But it doesnt work. It doesnt show any error, it simply doesnt do anything.

Do you maybe know how to fix it and replace the XYX from the text box which is put as the body of the email for the values from the E Column?
Are you using a reference to the Outlook object model or are you using CreateObject?
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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