Send automatic email from excel

samaco

New Member
Joined
Jun 6, 2023
Messages
8
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I have an excel sheet including clients' names, email addresses and phone numbers. I need to contact each one twice a month.
We are using a template that needs to be send biweekly to each client. I need also, excel to create a new cell says sent plus the date and time.

What is the possible way to configure this please?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I'm not sure what your template is. I'm assuming HTML.
Loop through the table send the email and write the date/time.

This should give you a start.

VBA Code:
Private Sub GenerateEmailFromAccountTable()
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim rng As ListRow
    Dim accountName As String
    Dim templateAsHTML As String
    Dim i As Long
    
    Set ws = ThisWorkbook.Sheets("YourSheetName")
    Set tbl = ws.ListObjects("tblAccount")
    
    ' get the template
    templateAsHTML = GenerateHTML
    
    For i = 1 To tbl.ListRows.Count
        accountName = tbl.ListRows(i).Range(, 1).Value
        
        If Len(accountName) > 0 Then
            EmailAccount accountName, templateAsHTML
            
            tbl.ListRows(i).Range(, 2).Value = Now
        End If
    Next rng
End Sub

Private Function GenerateHTML() As String
    Dim html As String
    html = "<html><body><p>Your account is awesome!</p></body></html>"
    GenerateHTML = html
End Function

Private Sub EmailAccount(accountName As String, templateAsHTML As String)
    Dim app As Object
    Dim item As Object

    On Error GoTo eh
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Set app = CreateObject("Outlook.Application")
    Set item = app.CreateItem(0)

    If Len(accountName) > 0 Then
        With item
            .To = accountName
            .Subject = "Subject Line"
            .Body = templateAsHTML
            .send
        End With
    Else
        Err.Raise 1001, "EmailAccount", "Email is not set in admin, can't send"
    End If

    GoTo out
eh:
    ' do something with the error

out:

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    Set app = Nothing
    Set item = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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