从 Excel VBA 发送个性化电子邮件

Sending a personalized email from Excel VBA

有人会这么好心帮我解决我的问题吗?我有这个例子 table:

我想为每一行发送一封个性化的电子邮件,这是我目前收到的:

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

我想从电子邮件中获取 B 列和 C 列的数据,但我不知道如何在 For each 循环中引用它们以及如何将它们放到我想要的位置。

谢谢

相反,如果使用 Range Object,则将您正在使用的范围的内容存储到矩阵(二维数组)中 现在您可以通过索引数组来访问 "cells"。所以 B 列的内容将是 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

试试这个代码:(我在你的代码中更改了 3 行,我用 (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

像这样尝试。

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)

宏将遍历 "Sheet1" 中的每一行,如果 B 列中有 E-mail 地址 和 C:Z 列中的文件名,它将创建包含此信息的邮件并发送。

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