下列程式碼範例示範如何根據活頁簿中儲存的資料,將電子郵件傳送至收件者清單。 收件者電子郵件地址必須位於欄 A 中,而且電子郵件的本文必須位於使用中工作表的第一個文字方塊中。
範例程式碼提供者:Holy Macro! Books 所出版的 Holy Macro! It's 2,500 Excel VBA Examples
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! 專為使用 Microsoft Office 的人所發行的有趣書籍。 請於下列網站參閱完整目錄:MrExcel.com.
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。