XXLFormula AI
Pro ProjectAutomationIntermediateIncludes .xlsm

Email Automation

4.9· 5,210 learners

A safer, more flexible mail merge than Word: iterate a recipients list, build HTML bodies from cell values, attach per-row files, throttle sends, and write back a delivery timestamp for auditability.

Learners
5,210
Duration
2 hours
Difficulty
Intermediate
Email Automation.xlsm — VBA EditorFileEditViewInsertFormatDebugRunToolsAdd-InsWindowHelpRun Sub/UserForm (F5)Project — VBAProjectVBAProject (Budget Automation.xlsm)Microsoft Excel ObjectsSheet1 (Transactions)Sheet2 (Dashboard)Sheet3 (Settings)ThisWorkbookFormsfrmCategoryEditorModulesmodBudgetmodChartsmodUtilsClass ModulesclsTransactionProperties — modBudget(Name)modBudget(General) ▾    RefreshBudget ▾1Option Explicit23' Budget refresh — one-click month-end.4Public Sub RefreshBudget()5    Dim wsData As WorksheetwsDash As Worksheet6    Dim lastRow As Longi As Long78    On Error GoTo Fail9    Application.ScreenUpdating = False1011    Set wsData = ThisWorkbook.Worksheets("Transactions")12    Set wsDash = ThisWorkbook.Worksheets("Dashboard")13    lastRow = wsData.Cells(wsData.Rows.Count"A").End(xlUp).Row1415    For i = 2 To lastRow16        Call AddToCategory(wsDash_17            CStr(wsData.Cells(i"C").Value), _18            CCur(wsData.Cells(i"D").Value))19    Next i2021    Application.ScreenUpdating = True22    MsgBox "Budget refreshed."vbInformation23    Exit Sub24Fail:25    Application.ScreenUpdating = True26    MsgBox "Failed: " & Err.DescriptionvbCritical27End SubImmediate — runningRefreshBudget?RefreshBudget › Loaded 218 transactions from 'Transactions' › Categories matched: 12 / 12 › Savings goals recalculated (6 goals) › Trend chart rebuilt in 42 ms Done. 0 errors, 0 warnings.ReadyLn 24, Col 5

Project screenshots

Code preview

Syntax-highlighted VBA — first 14 lines unlocked. Upgrade to view the full project.

email-automation.bas
VBA
1Option Explicit
2 
3' Email Automation — send personalised emails from a "Recipients" list
4' with per-row attachments and rate limiting.
5 
6Public Sub SendMailMerge()
7 Dim ws As Worksheet, i As Long, lastRow As Long
8 Dim OutApp As Object, OutMail As Object
9 Dim toAddr As String, subject As String, body As String
10 
11 On Error GoTo Fail
12 Set ws = ThisWorkbook.Worksheets("Recipients")
13 lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
14 
15 Set OutApp = CreateObject("Outlook.Application")
16 
17 For i = 2 To lastRow
18 toAddr = CStr(ws.Cells(i, "A").Value)
19 subject = CStr(ws.Cells(i, "B").Value)
20 body = BuildBody(ws, i)
21 
22 Set OutMail = OutApp.CreateItem(0)
23 With OutMail
24 .To = toAddr
25 .Subject = subject
26 .HTMLBody = body
27 If Len(ws.Cells(i, "D").Value) > 0 Then .Attachments.Add CStr(ws.Cells(i, "D").Value)
28 .Send
29 End With
30 
31 ws.Cells(i, "E").Value = "Sent " & Now
32 Application.Wait Now + TimeSerial(0, 0, 2) ' rate limit
33 Next i
34 
35 MsgBox lastRow - 1 & " emails sent.", vbInformation
36 Exit Sub
37Fail:
38 MsgBox "Mail merge failed: " & Err.Description, vbCritical
39End Sub
Locked preview

Unlock XLFormula AI Pro to view the complete project source, download the .xlsm workbook, and generate custom VBA with AI.

Unlock with Pro

Key features

  • HTML body built from cell values
  • Per-row attachments
  • Configurable rate limit
  • Delivery timestamp write-back
  • Preview mode — send to yourself first

What's included

  • Email Automation.xlsm
  • Sample recipient list (25 rows)
  • HTML body template
  • Setup guide (PDF)
Included with Pro

Get this project — and every other one

Every download, every VBA project, every future release for $5.00/month.

  • AI VBA Generator
  • Download Projects
  • Complete VBA Library
  • Lifetime Updates
  • Premium Templates
  • Priority Support

Frequently asked questions

Will Outlook block it as spam?
The 2-second default rate limit keeps you well under Outlook's throttle. Adjust in the Settings tab.
Can I preview before sending?
Yes — Preview Mode sends every message to your own address so you can QA before switching to live.

Related projects