excel宏编写指令 (excel宏怎么自动发邮件)

Excel VBA 能够使用 Outlook 客户端来发送电子邮件。下面提供一个简单的示例代码:

Sub SendEmail()

Dim OutApp As Object

Dim OutMail As Object

Dim strto As String, strcc As String, strbcc As String, strsub As String, strbody As String

Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)

strto = "[email protected]; [email protected]"

strcc = "[email protected]"

strbcc = "[email protected]"

strsub = "邮件主题"

strbody = "邮件正文"

On Error Resume Next

With OutMail

.To = strto

.CC = strcc

.BCC = strbcc

.Subject = strsub

.HTMLBody = strbody

.Display '或 .Send 发送

End With

On Error GoTo 0

Set OutMail = Nothing

Set OutApp = Nothing

End Sub

在上述代码中,您能够将电子邮件的收件人、抄送、密送、主题和正文等信息替换为您实际需要的信息。另外,通过 .Display 方法,您能够启动 Outlook 客户端并显示电子邮件,这样您能够检查邮件内容并手动发送。如果您想自动发送,只需用 .Send 方法替换 .Display 即可。