VBA to loop when sending email

crook_101

Well-known Member
Joined
Oct 20, 2008
Messages
687
Hi all,

I have inherited an excel sheet for our local group that uses VB to send emails - I am an old rusty coder and have cobbled together some code that runs, generates & sends an email. What it doesn't do and I would like it to do is to loop through column BR (If Sheet3.Cells(i, 70).Value = Date Then) looking for all populated date fields and then send the relevant email. Any nudges in the right direction / code would be much appreciated.

Many thanks from a rusty guy just getting back into Excel & VB after retirement!!

VBA Code:
Public Sub IJSendMailTo()

Dim sender As String
Dim name As String
Dim address As String
Dim subject As String
Dim body As String
Dim bodyFormat As Integer
Dim i As Integer

For i = 2 To Sheet3.Cells(Rows.Count, 1).End(xlUp).Row
    
        If Sheet3.Cells(i, 70).Value = Date Then

sender = "noreply@northumbriabloodbikes.org.uk"
name = "AlanK"
address = Sheet3.Cells(i, 7).Value ' the TO address
subject = "NBB - Invitation To Arrange Your Annual Driver Assessment"
body = "<Body style = font-size:12pt; font-family:Arial>" & "Hi " & Sheet3.Cells(i, 3) & "," & "<br>NBB ID:- " & Sheet3.Cells(i, 1) _
                & "<br><br>Your Annual Driver Assessment is/was due on  <strong>" & Sheet3.Cells(i, 62) & ".</strong><br>" _
    & "<strong><p style=color:red;>" & "Do not book a Car Shift after this date unless your Assessment is completed as you will not be insured.</strong><br><br>" _
    & "<p style=color:black;>" & "Please would you arrange your assessment with one of the Assessors below.<br><br>" _
    & "   ( Durham City )<br><br>" _
    & "   ( Alnwick )<br><br>" _
    & "   ( Ryton )<br><br>" _
    & "   ( Sunderland )</p>" _
    & "Thank you for your continued commitment and support."
        
End If
Next

bodyFormat = 1

    Set oSmtp = New EASendMailObjLib.Mail
    oSmtp.LicenseCode = "TryIt" ' Here goes your license code for the software; for now, we are using the trial version

    ' Please change the server address, username, and password to the ones you will be using       
    ' ALK The KUALO (NBB's) email creds
    
    oSmtp.ServerAddr = "XXXXXX.net"
    oSmtp.UserName = "noreply@northumbriabloodbikes.org.uk"
    oSmtp.Password = "XXXXX"

    oSmtp.ServerPort = 465

    oSmtp.ConnectType = 3
    oSmtp.FromAddr = sender
    oSmtp.AddRecipient name, address, 0
    oSmtp.subject = subject
    oSmtp.bodyFormat = bodyFormat
    oSmtp.BodyText = body

oSmtp.Asynchronous = 1
oSmtp.SendMail
Set oSmtp = Nothing
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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