簡體   English   中英

如何使用Excel VBA發送電子郵件

[英]How to send email using excel VBA

我的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

在下面的部分中,您嘗試在其中獲取最后一行,然后獲取該單元格中的電子郵件:

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

您從第一行獲取Email ,然后在Do Until循環中檢查最后一行, 但不修改 Email變量。

解決 :無需循環即可在A列中找到具有有效電子郵件地址的最后一行,您只需在下面的幾行中即可找到它:

Dim LastRow As Long

' get last row with data in Column A (don't skip blank cells in the middle)
LastRow = Sheet1.Range("A1").End(xlDown).Row
Email = Sheet1.Range("A" & LastRow).Value

您的其余代碼工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM