Sending multiple emails using VBA

Tej92

Board Regular
Joined
Sep 27, 2022
Messages
73
Office Version
  1. 365
Platform
  1. Windows

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Managed to do it, here is the code below on how i got it to work.

Private Sub CommandButton1_Click()
'declare variables
Dim i As Long
Dim lastRow As Long
Dim weekNum As Long
Dim area As String
Dim emailAddr As String
Dim CDO_Mail As Object
Dim CDO_Config As Object
Dim CDO_Fields As Object
Dim ws As Worksheet

'set the worksheet to work with
Set ws = ThisWorkbook.Sheets("Sheet1") 'Change "Sheet1" to the name of your worksheet

'get the week number from cell B12
weekNum = ws.Range("B12").Value

'initialize CDO configuration
Set CDO_Config = CreateObject("CDO.Configuration")

'set the CDO configuration properties
With CDO_Config.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your smtp server" ' smtp server address
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = xx ' server port
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With

'loop through the email addresses in the column that corresponds to the week number
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
For i = 14 To lastRow
'get the area and email address
area = ws.Range("A" & i).Value
emailAddr = ws.Cells(i, weekNum + 1).Value

'create a new email
Set CDO_Mail = CreateObject("CDO.Message")

'set the email properties
With CDO_Mail
.Subject = "Subject of the email"
.From = "noreply@mail.com" 'replace with your email address
.To = emailAddr
.TextBody = "Dear " & emailAddr & "," & vbCrLf & vbCrLf & _
"This is the body of the email. " & area & "." & vbCrLf & vbCrLf & _
"Regards," & vbCrLf & "Your Name"
.Configuration = CDO_Config
.Send
End With

'release the memory
Set CDO_Mail = Nothing
Next i

'release the memory
Set CDO_Config = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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