下面的代码示例演示如何根据工作簿中存储的数据向收件人列表发送电子邮件。 收件人电子邮件地址必须位于 A 列中,并且电子邮件的正文文本必须位于活动工作表的第一个文本框中。
示例代码提供方:Holy Macro! Books 出版的 Holy Macro! It's 2,500 Excel VBA Examples(Holy Macro! 2,500 个 Excel VBA 示例)
Sub Sample()
'Setting up the Excel variables.
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
'Create the Outlook application and the empty email.
Set olApp = CreateObject("Outlook.Application")
Set olMailItm = olApp.CreateItem(0)
'Using the email, add multiple recipients, using a list of addresses in column A.
With olMailItm
SDest = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(1))
If SDest = "" Then
SDest = Cells(iCounter, 1).Value
Else
SDest = SDest & ";" & Cells(iCounter, 1).Value
End If
Next iCounter
'Do additional formatting on the BCC and Subject lines, add the body text from the spreadsheet, and send.
.BCC = SDest
.Subject = "FYI"
.Body = ActiveSheet.TextBoxes(1).Text
.Send
End With
'Clean up the Outlook application.
Set olMailItm = Nothing
Set olApp = Nothing
End Sub
关于参与者
Holy Macro! Books 主要出版娱乐书籍,供使用 Microsoft Office 的人员阅读。 有关完整目录,请访问 MrExcel.com。
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。