Generating Automatic Emails For A List With Different Values

Tuanphun

New Member
Joined
May 16, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to create something to email folks that have received an error. I have gotten started on the code below but don’t know how to finish. I want the emails to be sent to their email address in column B. In the body of the email I want it to say Hello to their name in column A along with a short email that pulls the error in column C. I have it set to display the emails because I like to look at it before I hit the send button just to make sure. I would also like to just have this layout created so I can change the names and errors. How do I complete this with the code below?

Sub SendEmail_DualComp()

Dim EmailApp As Outlook.Application
Set EmailApp = New Outlook.Application

Dim EmailItem As Outlook.MailItem
Set EmailItem = EmailApp.CreateItem(olMailItem)
Dim SigString As String
Dim Signature, EmailTo, ccTo, subj, msg, Filepath As String
Dim ws As Worksheet
Dim Cel As Range
Dim LR As Long
Dim rng As Range

Set ws = Sheets("Sheet1")


EmailItem.To = ActiveSheet.Range("A1").Value
EmailItem.Subject = "Dual Compensation Audit Results 2021"

EmailItem.Body = "Good Morning," & vbNewLine & vbNewLine & "This is my first email from Excel" & _
vbNewLine & vbNewLine & _
"Thank You," & vbNewLine & vbNewLine & _



EmailItem.Display

On Error GoTo 0
Set Mail = Nothing
Set outApp = Nothing



End Sub
 

Attachments

  • Comp.jpg
    Comp.jpg
    25 KB · Views: 12

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.
VBA Code:
Option Explicit

Sub Create_Mail_From_List()
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" Then
            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = Cells(cell.Row, "B").Value
                .Body = "Dear " & Cells(cell.Row, "A").Value _
                      & vbNewLine & vbNewLine & _
                        "The following error has been noted : " & Cells(cell.Row, "C").Value
                .Display
                '.Send  'Or use Display.
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
VBA Code:
Option Explicit

Sub Create_Mail_From_List()
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" Then
            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = Cells(cell.Row, "B").Value
                .Body = "Dear " & Cells(cell.Row, "A").Value _
                      & vbNewLine & vbNewLine & _
                        "The following error has been noted : " & Cells(cell.Row, "C").Value
                .Display
                '.Send  'Or use Display.
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub
Thank you so much for your time. This actually worked amazingly well. I also appreciate the cell check in there to make sure it is an email.
 
Upvote 0

Forum statistics

Threads
1,214,393
Messages
6,119,261
Members
448,880
Latest member
aveternik

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