VBA Help: Email loop with an attachment

NeoSez

Board Regular
Joined
Aug 14, 2020
Messages
210
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I went to the Ron de Bruin site and it's no longer there😭.
Can someone please help me with the VBA code to send emails?
I want to send emails to all the people in my contacts list of like 1000.
In the body, it will say Hello (name of company from column "A")
I have a file (abc.pdf) I would like to attach to each email.
Subject line should have today's date at the end of the line. Eg. Delivery Date (today's date)
Thank you.

ABCDE
Companyline numbering starting from 2. This is used so I can start at any number for emailing to that company. Eg. I might want to omit email the first 10 people and start at #11 instead. But not necessary if it can't be done.TO: (email addresses)CC: (email addresses)
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
paste code into a module

'NOTE : YOU MUST HAVE THE OUTLOOK X.x Object Library REFERENCE CHECKED IN VBE; Alt-F11, menu,tools, references, Microsoft Outlook XX Object library

this assumes Co Name is col A
email is in col B
filepath to attach is in .J1
adjust as needed

have outlook open
then run: SendAllEmails

Code:
Public Sub SendAllEmails()
Dim i As Integer
Dim vEmail, vFile, vCC
Dim vName, vSubj, vBody

'MsgBox "Have Outlook open."
'SetWarnings False

  'the file path for file to attach is in J1.
vFile = Range("J1").Value

Sheets("Contacts").Activate    'goto the email list
  
        'cycle thru the list of email addrs
Range("A2").Select
While ActiveCell.Value <> ""
 
  vName = ActiveCell.Offset(0, 0).Value   'col A
  vEmail = ActiveCell.Offset(0, 1).Value   'col B
  vSubj = " Delivery Date (" & Date & ")"
  vBody = "Hello " & vName
 
  Send1Email vEmail, vSubj, vBody,  vFile

  ActiveCell.Offset(1, 0).Select   'next row
Wend

'SetWarnings True
MsgBox "Done"

End Sub


Private Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody,  Optional ByVal pvFile) As Boolean
Dim oMail As Outlook.MailItem
Dim oApp As Outlook.Application

On Error GoTo ErrMail

'NOTE : YOU MUST HAVE THE OUTLOOK X.x Object Library    REFERENCE CHECKED IN VBE; Alt-F11, menu,tools, references, Microsoft Outlook XX Object library

Set oApp = GetApplication("Outlook.Application")  'it may be open already so use this
'Set oApp = CreateObject("Outlook.Application")  'not this

Set oMail = oApp.CreateItem(olMailItem)
With oMail
    .To = pvTo
    .Subject = pvSubj
 
    If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
 
    .HTMLBody = pvBody
 
    '.Display True
    .Send
End With

Send1Email = True
endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function

ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume endit
Resume
End Function


Private Function GetApplication(className As String) As Object
' function to encapsulate the instantiation of an application object
Dim theApp As Object
On Error Resume Next
Set theApp = GetObject(, className)
If Err.Number <> 0 Then
    MsgBox "Unable to Get" & className & ", attempting to CreateObject"
    Set theApp = CreateObject(className)
End If

If theApp Is Nothing Then
    Err.Raise Err.Number, Err.Source, "Unable to Get or Create the " & className & "!"
    Set GetApplication = Nothing
End If

'MsgBox "Successfully got a handle on Outlook Application, returning to caller"
Set GetApplication = theApp
End Function
 
Last edited:
Upvote 1
Solution
paste code into a module

'NOTE : YOU MUST HAVE THE OUTLOOK X.x Object Library REFERENCE CHECKED IN VBE; Alt-F11, menu,tools, references, Microsoft Outlook XX Object library

this assumes Co Name is col A
email is in col B
filepath to attach is in .J1
adjust as needed

have outlook open
then run: SendAllEmails

Code:
Public Sub SendAllEmails()
Dim i As Integer
Dim vEmail, vFile, vCC
Dim vName, vSubj, vBody

'MsgBox "Have Outlook open."
'SetWarnings False

  'the file path for file to attach is in J1.
vFile = Range("J1").Value

Sheets("Contacts").Activate    'goto the email list
 
        'cycle thru the list of email addrs
Range("A2").Select
While ActiveCell.Value <> ""
 
  vName = ActiveCell.Offset(0, 0).Value   'col A
  vEmail = ActiveCell.Offset(0, 1).Value   'col B
  vSubj = " Delivery Date (" & Date & ")"
  vBody = "Hello " & vName
 
  Send1Email vEmail, vSubj, vBody,  vFile

  ActiveCell.Offset(1, 0).Select   'next row
Wend

'SetWarnings True
MsgBox "Done"

End Sub


Private Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody,  Optional ByVal pvFile) As Boolean
Dim oMail As Outlook.MailItem
Dim oApp As Outlook.Application

On Error GoTo ErrMail

'NOTE : YOU MUST HAVE THE OUTLOOK X.x Object Library    REFERENCE CHECKED IN VBE; Alt-F11, menu,tools, references, Microsoft Outlook XX Object library

Set oApp = GetApplication("Outlook.Application")  'it may be open already so use this
'Set oApp = CreateObject("Outlook.Application")  'not this

Set oMail = oApp.CreateItem(olMailItem)
With oMail
    .To = pvTo
    .Subject = pvSubj
 
    If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
 
    .HTMLBody = pvBody
 
    '.Display True
    .Send
End With

Send1Email = True
endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function

ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume endit
Resume
End Function


Private Function GetApplication(className As String) As Object
' function to encapsulate the instantiation of an application object
Dim theApp As Object
On Error Resume Next
Set theApp = GetObject(, className)
If Err.Number <> 0 Then
    MsgBox "Unable to Get" & className & ", attempting to CreateObject"
    Set theApp = CreateObject(className)
End If

If theApp Is Nothing Then
    Err.Raise Err.Number, Err.Source, "Unable to Get or Create the " & className & "!"
    Set GetApplication = Nothing
End If

'MsgBox "Successfully got a handle on Outlook Application, returning to caller"
Set GetApplication = theApp
End Function
Thank you for providing the code so quickly. There were a few errors and compile error: user-defined type not defined, was one of the errors. I made the changes below:
Private Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
Dim oMail As Object
Dim oApp As Object
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)

End Function
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,063
Members
449,090
Latest member
fragment

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