发送大量带有循环和排除项的电子邮件?

时间:2019-05-02 20:15:48

标签: excel vba email

我正在尝试使用Do ... Until Loop发送电子邮件。我完全没有VBA的经验,我只是想弄清楚如何从excel发送电子邮件,并使代码停在空白处或0(因为我正在使用其他工作簿的引用来保持更新)

我也尝试将其更改为For ... Next语句,但这没有用。

Sub SendEmail(what_address As String, subject_line As String, mail_body 
    As String)

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

olMail.To = what_address
olMail.Subject = subject_line
olMail.BodyFormat = olFormatHTML
olMail.HTMLBody = mail_body
olMail.Send


End Sub

Sub SendMassEmail()

row_number = 1

Do
DoEvents
  row_number = row_number + 1
  Dim mail_body_message As String
  Dim full_name As String
  Dim policy_number As String
  Dim address As String
  Dim city As String
  Dim day As String
  Dim web_address As String

  mail_body_message = Sheet5.Range("I2")
  full_name = Sheet5.Range("B" & row_number) & " " & Sheet5.Range("C" & 
    row_number)
  policy_number = Sheet5.Range("F" & row_number)
  address = Sheet5.Range("D" & row_number)
  city = Sheet5.Range("E" & row_number)
  day = Sheet5.Range("G" & row_number)
  web_address = Sheet5.Range("H1")
  mail_body_message = Replace(mail_body_message, "replace_name_here", 
    full_name)
  mail_body_message = Replace(mail_body_message, "policy_number_replace", 
    policy_number)
  mail_body_message = Replace(mail_body_message, "day_replace", day)
  mail_body_message = Replace(mail_body_message, "address_replace", 
    address)
  mail_body_message = Replace(mail_body_message, "city_replace", city)
  mail_body_message = Replace(mail_body_message, "web_replace", 
    web_address)
  Call SendEmail(Sheet5.Range("A" & row_number), "Insurance update", 
    mail_body_message)
Loop Until row_number = 2

MsgBox "Complete!"

End Sub

理想情况下,这将读取所有行并发送与我的电子邮件正文相关的电子邮件。它将从我的参考公式和没有数据的空格中排除0。当我没有手动设置循环的row_number时,我总是遇到运行时错误。它会通过电子邮件发送邮件,但会阻止auto_open子邮件运行

1 个答案:

答案 0 :(得分:-1)

Dim row_number As Integer

丢失