Adding signature to for loop email creaton

godzilla185

New Member
Joined
Sep 27, 2021
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a very useful code I made for work, which does the following:

1. removes duplicate client names
2. inserts contact list
3. Vlookup today's client names against the contact list to make the next step for client emails dynamic.
4. Generates emails to each client and inserts their corresponding excel files.

I got everything to work except the email signature in #4 (removed from code to show what is working so far)

The code for the email part is below (removed private information which is not relevant):
Columns H, I, J, and K hold respectively the "To", "Subject", "Body" and excel files to attach parts of the email.
I have looked at rondebruin and on this forum to add the signature part of the code and cannot get it to work. Am wondering if anyone can help.

...
Sub Email_and_AttachFile()

Dim r As Long
Dim m As Long
Dim sMail As String
Dim sFile As String
Dim oApp As Object
Dim oMsg As Object

Dim wd As Date
wd = WorksheetFunction.WorkDay(Date, -1)

Set oApp = CreateObject(Class:="Outlook.Application")
m = Range("K" & Rows.Count).End(xlUp).Row
For r = 2 To m

sFile = Range("K" & r).Value
Set oMsg = oApp.CreateItem(0)
With oMsg
.To = Range("H" & r).Value
.Subject = Range("I" & r).Value
.Body = "Good morning," & vbNewLine & Format(wd, "dd mmm yyyy") _
& Range("J" & r).Value & vbNewLine & "Many thanks," & vbNewLine & Signature

.CC = "bob”"
.Attachments.Add sFile
.Display

End With

Next r

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi. Which method did you try of Ron de Bruin's? The first one will open an email up first, which would automatically populate the default email signature, and then adds the pertinent information (i.e.; To, Subject, Body, etc.). The second method requires knowing the name of the signature file, which is usually a problem when more than one person is using the code to generate emails.

I adapted your code using Ron's first method below. Hopefully that works out for you.

VBA Code:
Sub Email_and_AttachFile()

Dim r As Long
Dim sMail As String, sFile As String
Dim oApp As Object: Set oApp = CreateObject(Class:="Outlook.Application")
Dim oMsg As Object

Dim wd As Date: wd = WorksheetFunction.WorkDay(Date, -1)
Dim m As Long: m = Range("K" & Rows.Count).End(xlUp).Row
Dim strbody As String

For r = 2 To m

    sFile = Range("K" & r).Value
    Set oMsg = oApp.CreateItem(0)
    
    strbody = "Good morning, <br><br>" & Format(wd, "dd mmm yyyy") & "<br><br>" & _
              "Let me know if you have problems.<br>" & Range("J" & r).Value & _
              "<br><br>Many thanks,"

    With oMsg
        .Display
        .To = Range("H" & r).Value
        .Subject = Range("I" & r).Value
        .HTMLBody = strbody & "<br>" & .HTMLBody
        .CC = "bob"
        .Attachments.Add sFile
        .Display
    End With

Next r

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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