将Excel链接到电子邮件。发送批量电子邮件

时间:2017-02-11 23:05:51

标签: excel vba excel-vba email email-integration

我试图批量发送电子邮件(我认为Excel会是一个很好的选择)。基本上尝试将Excel文件桥接到电子邮件地址并自动填写3或4个空格。例如:Hello [Insert cell A1] blah blah blah [Insert cell B1]。

只是想知道从哪里开始。 (我知道这需要一些VBA)

1 个答案:

答案 0 :(得分:2)

这是您可以使用的基本模板:

Option Explicit

Dim IE As Object

Sub Send_Email()
  Dim eSubject, eTo, eBody As String
  Dim Mail_Object, Mail_Single As Variant
  eSubject = "Example of how to send email using VBA"
  eTo = "your_username@gmail.com"

  Dim sht As Worksheet
  Set sht = Sheets("Sheet1")
  With sht:
    eBody = "Hello " & .Cells(1, "A").value & " blah blah blah " & .Cells(1, "B").value
  End With

  On Error GoTo debugHere
  Set Mail_Object = CreateObject("Outlook.Application")
  Set Mail_Single = Mail_Object.CreateItem(0)
  With Mail_Single
   .Subject = eSubject
   .To = eTo
   .body = eBody
   .send
  End With

debugHere:
  If Err.Description <> "" Then MsgBox Err.Description 
End Sub

要使用此模板发送多封电子邮件,您只需在代码中放置一个for循环,该代码以设置Mail_Object开头,以end with statement结束。