如何使用excel VBA发送电子邮件

时间:2017-01-01 10:30:22

标签: excel-vba vba excel

我的数据包含在A到H列中.A列中的数据是电子邮件地址。我在另一个单元格中放了一个复选框。我想要的是当我勾选复选框时,它继续向最后一行A列中的电子邮件帐户发送电子邮件。但以下代码仅适用于单元格A1。

Private Sub CheckBox1_Click()

    Dim Email As String

    Row = 1
    Email = Sheet1.Cells(Row, 1)
    Do Until Sheet1.Cells(Row, 1) = ""
        Row = Row + 1
    Loop

    Dim OutApp As Object, OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
        .To = Email
        .Subject = score
        .HTMLBody = "This is a contain of Email Message"
        .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing

End Sub

1 个答案:

答案 0 :(得分:3)

在下面的部分中,您尝试获取最后一行,然后是该单元格中的电子邮件:

Row = 1
Email = Sheet1.Cells(Row, 1)
Do Until Sheet1.Cells(Row, 1) = ""
    Row = Row + 1
Loop

您从第一行开始Email,在Do Until循环后检查最后一行但未修改 {{1}变量。

解决:无需循环查找A列中有效电子邮件地址的最后一行,您可以通过以下以下行找到它:

Email

其余代码运行正常。