VBA to Send Mass Emails with Individual Attachments

MrMaker

Board Regular
Joined
Jun 7, 2018
Messages
56
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I need some VBA to allow me to send multiple emails (same standard email body but each with their own individual Excel attachment).

I'm using Microsoft Outlook and Excel 365

Workbook Entitled: 'Raw Data'
Sheet Entitled 'Sheet1'
Column A: Email Address
Column B: First Name (for email greeting)
Column C: Attachment Path (.xlsx files to be sent)

The Email Body has some text and a couple of links, so I can set up a message template if easier?

Any help would be much appreciated, nothing I have found online to date appears to work

Thank you
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
In the cells of column C you have something like this:

c:\job\books\bookxd.xlsx

That is, you have the folder and the file name, if so then try this:

VBA Code:
Sub Send_Mass_Emails()
  Dim dam As Object, i As Long, sFile As String
  
  For i = 2 To Range("a" & Rows.Count).End(3).Row
    Set dam = CreateObject("outlook.application").createitem(0)
    dam.To = Range("A" & i).Value
    dam.Subject = "Subject " & Range("B" & i).Value
    dam.body = "The Email Body has some text and a couple of links "
    sFile = Range("C" & i).Value
    If Dir(sFile) <> "" Then dam.Attachments.Add
    dam.Display 'change to .Send to send
  Next
End Sub
 
Upvote 0
Thank you for your reply.

I am getting 'Automation Error' when I run it though?
 
Upvote 0
Run the macro again and when the error appears press the "Debug" button and write here which line is highlighted in yellow.
What else does the error say?
 
Upvote 0
No debug error just the below box:

1600097381062.png
 

Attachments

  • 1600097360492.png
    1600097360492.png
    12.7 KB · Views: 15
Upvote 0
Maybe it's the excel version, wait for someone to put a code for office 365.
 
Upvote 0
Try this code:

VBA Code:
Sub Macro_email()  
   Dim rng As Range, Cell As Range, I As Long, DataRow As Long
   
   Set rng = Worksheets("BOOKING").Range("D6,D8,D10,D13,D15,D17,D19,D20")
   ' DataRow = Worksheets("MHELOAN").Columns(1).Find(What:=vbNullString, LookAt:=xlWhole, After:=Worksheets("MHELOAN").Cells(2, 1)).Row
   
   I = 1
   For Each Cell In rng
     ' Worksheets("MHELOAN").Cells(DataRow, I) = Cell.Text
      I = I + 1
   Next Cell
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = " COLLECTION REQUEST" & vbNewLine & vbNewLine & _
              "PLEASE BE ADVISED  BE COLLECTED" & vbNewLine & _
              "PLEASE CAN YOU MAKE ARRANGEMENT TO COLLECT AND PROCESSED"
                  On Error Resume Next
    With xOutMail
        .To = "team"
        .CC = ""
        .BCC = ""
        .Subject = "COLLECTION REQUEST"
        '.HTMLBody = RangetoHTML(rng)
        .Body = xMailBody & rng
        .display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,127
Members
453,021
Latest member
Justyna P

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