然后,就是编写VBA代码的部分了。VBA(Visual Basic for Applications)是微软为Office软件设计的脚本语言,可以帮助我们自动化一些复杂的操作。在Excel中,打开“开发工具”选项卡,点击“Visual Basic”,然后新建一个模块。在新的模块中,输入以下代码:
vba
Sub SendEmail()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim cell As Range
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set rng = Selection
strbody = "邮件正文内容" ' 这里填写你的邮件正文
For Each cell In rng
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "邮件主题" ' 这里填写你的邮件主题
.Body = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Next cell
Set OutApp = Nothing
End Sub
这段代码的作用是遍历你选择的区域,并为每一个邮件地址创建一个Outlook邮件,设置邮件的主题和内容,然后发送出去。