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.
Syntax-highlighted VBA — first 14 lines unlocked. Upgrade to view the full project.
1Option Explicit23' Email Automation — send personalised emails from a "Recipients" list4' with per-row attachments and rate limiting.56Public Sub SendMailMerge()7 Dim ws As Worksheet, i As Long, lastRow As Long8 Dim OutApp As Object, OutMail As Object9 Dim toAddr As String, subject As String, body As String1011 On Error GoTo Fail12 Set ws = ThisWorkbook.Worksheets("Recipients")13 lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row1415 Set OutApp = CreateObject("Outlook.Application")1617 For i = 2 To lastRow18 toAddr = CStr(ws.Cells(i, "A").Value)19 subject = CStr(ws.Cells(i, "B").Value)20 body = BuildBody(ws, i)2122 Set OutMail = OutApp.CreateItem(0)23 With OutMail24 .To = toAddr25 .Subject = subject26 .HTMLBody = body27 If Len(ws.Cells(i, "D").Value) > 0 Then .Attachments.Add CStr(ws.Cells(i, "D").Value)28 .Send29 End With3031 ws.Cells(i, "E").Value = "Sent " & Now32 Application.Wait Now + TimeSerial(0, 0, 2) ' rate limit33 Next i3435 MsgBox lastRow - 1 & " emails sent.", vbInformation36 Exit Sub37Fail:38 MsgBox "Mail merge failed: " & Err.Description, vbCritical39End Sub
Unlock XLFormula AI Pro to view the complete project source, download the .xlsm workbook, and generate custom VBA with AI.
Unlock with ProEvery download, every VBA project, every future release for $5.00/month.
Generate a PDF invoice from a template, email it via Outlook, and log every invoice to a register.
Recursively iterate every workbook in a folder and consolidate their data into a single sheet.
Refresh every PivotTable and chart, then export a snapshot PNG for the daily report.