在VBA中实现通过Outlook发送邮件
1.添加引用Microsoft Outlook 11.0 Object Library。(这里用的是Microsoft Outlook 2003)
2.定义发送邮件过程。
Public Sub SendEmail(Receiver As String, SubjectText As String, BodyText As String, AttachedObject As String)
Dim OutlookApp As Outlook.Application
Dim OutlookItem As Outlook.MailItem
Set OutlookApp = New Outlook.Application
Set OutlookItem = OutlookApp.CreateItem(olMailItem)
On Error GoTo SendEmail_Error
With OutlookItem
.To = Receiver '收件人地址
.Subject = SubjectText '邮件主题
.Body = BodyText '邮件内容
.Attachments.Add AttachedObject '粘贴附件
.Send '发送邮件
End With
On Error GoTo SendEmail_Error
SendEmail_Exit:
Exit Sub
SendEmail_Error:
MsgBox Err.Description
Resume SendEmail_Exit
End Sub
SendEmail_Error:
End Sub
3.调用发送邮件过程。
4.在Outlook中添加新电子邮件帐户。