Email using VBA but changing a few words in Body depending on Recipient

Alphaboss7

New Member
Joined
Jul 31, 2017
Messages
28
Hello,

I need to send out a standardized email, but I need to change a few fields within the body of the email to reflect names specific to the recipient. It's pretty simple, but I can't find anything specific to this case.

Example:

Hello _______,

Here are your login credentials:

Username: _________
Password: ________

Thank you.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
.
Code:
Option Explicit


Sub CreateMail()
    Dim objOutlook      As Object
    Dim objMail         As Object
    Dim i               As Integer
    Const olMailItem    As Long = 0
                                    
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row '~~> Change to i = 2 if you have headings
        Set objOutlook = CreateObject("Outlook.Application")
        Set objMail = objOutlook.CreateItem(olMailItem)
                                        
        With objMail '~~> Chnage Below columns to suit your data, this was based off your example offsets
            .To = Range("F" & i).Value
            .cc = Range("G" & i).Value
            .Subject = Range("J" & i).Value
            '.Attachments.Add Range("I" & i).Value
            .HTMLBody = RangetoHTML(Range("K" & i)) '~~> You can change this to any range
            .display
        End With
                                        
        Set objOutlook = Nothing
        Set objMail = Nothing
    Next i
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook


    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With


    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")


    'Close TempWB
    TempWB.Close savechanges:=False


    'Delete the htm file we used in this function
    Kill TempFile


    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function



A
B
C
D
E
F
G
H
I
J
K
L
M
N
1
NamesToCCAttachmentsSubjectMsg Body
2
A2me@yahoo""ByeI'm leaving
3
A3You@yahoo""HelloNice to meet you
4
A4him@yahoo""What ?Please repeat
5
6
7
 
Last edited:
Upvote 0
This is great, but the msg body will be the same for all recipients, so the names need to change with the same message. And I would like to make sure the body of the email isn't stretched out to the right, as in, it would be nice to have multiple lines.

Example:

Hello, __Name__

Here are your user credentials:

Username: __Username Field__
Password: __Password Field__

Let me schedule a training for you.
Do you have time this week?

Let me know at your earliest convenience.

Thanks,
Signature
 
Upvote 0
.
Code:
Sub SendEmail()
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim email_ As String
    Dim cc_ As String
    Dim subject_ As String
    Dim body_ As String


     'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")


     'Loop through the rows
    For Each cell In Columns("a").Cells.SpecialCells(xlCellTypeConstants)


        email_ = cell.Offset(0, 1).Value
        subject_ = cell.Offset(0, 2).Value
        body_ = "Hello " & cell.Value & " :" & vbCrLf & vbCrLf & _
        "Here are your user credentials: " & vbCrLf & vbCrLf & _
        "Username: " & cell.Offset(0, 4).Value & vbCrLf & _
        "Password: " & cell.Offset(0, 5).Value & vbCrLf & vbCrLf & _
        "Let me schedule a training for you." & vbCrLf & _
        "Do you have time this week? " & vbCrLf & vbCrLf & _
        "Let me know at your earliest convenience. " & vbCrLf & vbCrLf & _
        "Thanks," & vbCrLf & _
        "Place Your Signature Here "
        
        cc_ = cell.Offset(0, 3).Value


        'Create Mail Item and send it
        Set MItem = OutlookApp.CreateItem(0)
        With MItem
            .To = email_
            .CC = cc_
            .Subject = subject_
            .Body = body_
            .Display
        End With
    Next
End Sub



A
B
C
D
E
F
G
H
1
Email AddressSubjectEmail in CC
UserName
Password
2
Marytest@test.comFill your time sheettest@testcc.com
xtz​
123​
3
Tomtest1@test.comFill your time sheettest1@testcc.com
123​
xyz​
4
Carltest2@test.comFill your time sheettest2@testcc.com
tdk​
cal​
5
6
Sallytest4@test.comFill your time sheettest4@testcc.com
546​
857​
7
8
Liztest6@test.comFill your time sheettest6@testcc.com
ghj​
kmj​
 
Upvote 0
Awesome, Thank you Logit. Now, do you know how to send a hyperlinked website in the body of the email?

And do you know how to include the outlook signature at the bottom?
 
Upvote 0
.
Code:
Option Explicit


Sub SendEmail()
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim email_ As String
    Dim cc_ As String
    Dim subject_ As String
    Dim body_ As String
    Dim Signature As String
    
    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")
    
    '*************************************************************
    Signature = Environ("appdata") & "\Microsoft\Signatures\"
    If Dir(Signature, vbDirectory) <> vbNullString Then
        Signature = Signature & Dir$(Signature & "*.htm")
    Else:
        Signature = ""
    End If
    
    Signature = CreateObject("Scripting.FileSystemObject").GetFile(Signature).OpenAsTextStream(1, -2).ReadAll
    
    '**************************************************************
    


     'Loop through the rows
    For Each cell In Columns("a").Cells.SpecialCells(xlCellTypeConstants)




        email_ = cell.Offset(0, 1).Value
        subject_ = cell.Offset(0, 2).Value
        body_ = "Hello " & cell.Value & " : <br></br> <br></br>" & _
        "Here are your user credentials:  <br></br> <br></br>" & _
        "Username: " & cell.Offset(0, 4).Value & "<br></br>" & _
        "Password: " & cell.Offset(0, 5).Value & "<br></br> <br></br>" & _
        "Let me schedule a training for you. <br></br>" & _
        "Do you have time this week? <br></br> <br></br>" & _
        "Let me know at your earliest convenience. <br></br> <br></br>" & _
        "Thanks,<br></br>" & _
        "Place Your Signature Here <br></br> <br></br>" & _
        "<a href=http://www.formatcells.com> formatcells.com.</a> <br></br> <br></br>" & _
        Signature
        
        cc_ = cell.Offset(0, 3).Value


        'Create Mail Item and send it
        Set MItem = OutlookApp.CreateItem(0)
        With MItem
            .To = email_
            .CC = cc_
            .Subject = subject_
            .HTMLBody = body_
            .Display
        End With
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,752
Messages
6,132,511
Members
449,731
Latest member
dasda34

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