VBA code to send separate emails to different people with different attachment

chchanbp

New Member
Joined
Apr 20, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I want to send email in Outlook to company (column E), their email is in column F, with attachment path in column H. What is the VBA code? Thanks!

1681973877027.png
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Something like this should work . . .
VBA Code:
    Dim olApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim Recipient, RecipientRange As Range

    Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
        Set olApp = New Outlook.Application
    End If
    Set RecipientRange = Range(Sheets("Sheet2").Cells(2,6),Sheets("Sheet2").Cells(Sheets("Sheet2").Cells(Rows.Count, 6).End(xlUp).Row, 6)) ' Assumes top row is header
    For Each Recipient In RecipientRange.Rows
    Set olMail = olApp.CreateItem(olMailItem)
    With olMail
    Do While .ReplyRecipients.Count > 0 ' Clear possible recipients
      .ReplyRecipients.Remove 1
    Loop
    .Recipients.Add LCase(Recipient.Value)
    .Attachments.Add Recipient.Offset(0,2).Value
    ' I will normally set breakpoint here and ensure my details are pulling & showing properly before allowing the send below
    .Display ' Show email to be sent 
    ' .Send ' Send email UNCOMMENT TO AUTO SEND
    End With
    DoEvents ' Allow for Events
    Sleep (3000) ' Wait 3 sec between each email
    DoEvents ' Allow for Events
    Next Recipient

    'Cleanup
    Set Recipient = Nothing
    Set RecipientRange = Nothing
    Set olMail = Nothing
    Set olApp = Nothing
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,834
Members
449,192
Latest member
mcgeeaudrey

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