0

Would anyone be so kind and help me out with my problem? I have this example table:

Excel Sheet

I would like to send a personalized email for each row, this is what I got so far:

Sub SendEmails()
   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("A").Cells.SpecialCells(xlCellTypeConstants)

        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
        With OutMail
            .To = cell.Value 
            .Subject = "Project" & Sheets("Sheet1").Range("C").Value        ' insert subject from column C
            .HTMLBody = "<p>Hello " & Sheets("Sheet1").Range("B").Value &"</p>" & _ ' insert Name from column B
            "<p><strong><u>This is a test email</u></strong></p>"
            .Display
        End With
        On Error GoTo 0
        Set OutMail = Nothing

   Next cell
   cleanup:
        Set OutApp = Nothing
        Application.ScreenUpdating = True
End Sub

I would like to have data from columns B and C in the email, but I have no idea how to reference them in For each loop and how to put them to the place I want.

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anthony
  • 27
  • 1
  • 3

3 Answers3

0

Instead if using a Range Object you store the content of the Range you are using into a matrix (2D Array) Now you can access the "cells" by indexing your array. So content of column B would be myArray(rowNumber,2)

Sub SendEmails()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Variant

myArray= ThisWorkbook.Sheets("Sheet1").Range("A1:C4")


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

For i = 2 To UBound(myArray)

        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = myArray(i, 1)
            .Subject = "Project" & myArray(i, 3)
            .HTMLBody = "<p>Hello " & myArray(i, 2) & "</p>" & _
            "<p><strong><u>This is a test email</u></strong></p>"
            .Display
        End With
Next i
FloLie
  • 1,820
  • 1
  • 7
  • 19
0

Try this code : (I changed 3 lines in your code, I marked Them with (X))

 Sub SendEmails()
       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("A").Cells.SpecialCells(xlCellTypeConstants)
            i = cell.Row '(X)
            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "Project" & Sheets("Sheet1").Range("C" & i).Value '(X)
                .HTMLBody = "<p>Hello " & Sheets("Sheet1").Range("B" & i).Value & "</p>" & "<p><strong><u>This is a test email</u></strong></p>" '(X)
                .Display
            End With
            On Error GoTo 0
            Set OutMail = Nothing
       Next cell
       cleanup:
            Set OutApp = Nothing
            Application.ScreenUpdating = True
  End Sub
LatifaShi
  • 440
  • 1
  • 3
  • 12
  • Thank you very much, nice and simple, solved the problem! – Anthony Apr 05 '18 at 12:51
  • You're Welcome @Anthony .. :) – LatifaShi Apr 05 '18 at 13:04
  • 1
    Just a remark to this solution: This works perfectly fine and for small numbers of recipients the performance is not much of an issue. However for large numbers you have 3*n reading operations in an excel sheet, which will become very inefficient – FloLie Apr 05 '18 at 13:25
0

Try it like this.

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

The Macro will loop through each row in "Sheet1" and if there is a E-mail address in column B and file name(s) in column C:Z it will create a mail with this information and send it.

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

https://www.rondebruin.nl/win/s1/outlook/amail6.htm

ASH
  • 20,759
  • 19
  • 87
  • 200