使用Excel VBA实现Outlook自动邮件发送的完整教程
创作时间:
作者:
@小白创作中心
使用Excel VBA实现Outlook自动邮件发送的完整教程
引用
CSDN
1.
https://blog.csdn.net/caixiaobaideye/article/details/140665718
本文将详细介绍如何使用Excel VBA实现Outlook自动邮件发送。通过创建一个支持宏的Excel文件,设置相关参数,并编写VBA代码,可以实现邮件的自动化发送。
1. 创建Excel文件
首先需要创建一个带有宏功能的Excel文件,文件后缀名为xlsm。这种文件格式支持宏的使用。
2. Excel操作
添加列
在Excel中添加以下列:
- 收件人:邮件接收者,多个收件人用分号(;)分隔
- 抄送人:抄送对象,多个抄送人用分号(;)分隔
- Outlook模板路径:保存的Outlook邮件模板路径,文件后缀为oft
- 替换内容:对模板中的内容进行替换,格式为:替换词1>替换内容1;替换词2>替换内容2
- 附件内容:添加附件的路径,多个附件用分号(;)分隔
- 插入图片:插入图片到指定位置,格式为:Image1>图片路径1;Image2>图片路径2
- 是否发送:设置邮件状态(1直接发送、0设置为草稿、2仅显示)
设置使用宏
要使用宏功能,需要在Excel中进行相应的设置:
进入Excel设置,勾选开发者窗口:
设置宏信任:
3. VBA代码编写
引用Outlook
发送代码
Sub SendEmail()
Dim smallMessenger As Outlook.Application
Set smallMessenger = New Outlook.Application
Dim newEmail As MailItem
Dim row, rows As Integer
Dim recipient As String
Dim ccRecipients As String
Dim subject As String
Dim outlookTemplatePath As String
Dim replacementContent As String
Dim attachmentContent As String
Dim insertImages As String
Dim sendDirectly As String
Dim strImageHTML As String
Dim i, j As Integer
Dim Before() As Variant
Dim Back() As Variant
Dim attachs() As String
rows = ActiveSheet.UsedRange.rows.Count
For i = 2 To rows
recipient = Cells(i, "A")
ccRecipients = Cells(i, "B")
subject = Cells(i, "C")
outlookTemplatePath = Cells(i, "D")
replacementContent = Cells(i, "E")
attachmentContent = Cells(i, "F")
insertImages = Cells(i, "G")
sendDirectly = Cells(i, "H")
Set newEmail = smallMessenger.CreateItemFromTemplate(outlookTemplatePath)
newEmail.To = recipient
newEmail.CC = ccRecipients
newEmail.subject = subject
' 鏇挎崲鍐呭
If replacementContent = "" Then
GoTo label1
End If
Before = getBefore(replacementContent)
Back = getBack(replacementContent)
For j = LBound(Before) To UBound(Before)
newEmail.HTMLBody = Replace(newEmail.HTMLBody, Before(j), Back(j))
Next
label1:
' 闄勪欢鍐呭
If attachmentContent = "" Then
GoTo label2
End If
attachs = Split(attachmentContent, ";")
For j = LBound(attachs) To UBound(attachs)
newEmail.Attachments.Add (attachs(j))
Next
label2:
'鎻掑叆鍥剧墖
If insertImages = "" Then
GoTo label3
End If
Before = getBefore(insertImages)
Back = getBack(insertImages)
For j = LBound(Before) To UBound(Before)
strImageHTML = "<img src='" & Back(j) & "'>"
newEmail.HTMLBody = Replace(newEmail.HTMLBody, Before(j), strImageHTML)
Next
label3:
If sendDirectly = 1 Then
newEmail.Send
ElseIf sendDirectly = 2 Then
newEmail.Display
ElseIf sendDirectly = 0 Then
newEmail.Close olSave
End If
Next
End Sub
Function getBefore(ByVal inputText As String) As Variant()
Dim tokens() As String
Dim result() As Variant
Dim curtokens() As String
Dim i As Integer
tokens = Split(inputText, ";")
ReDim result(0 To UBound(tokens))
For i = LBound(tokens) To UBound(tokens)
curtokens = Split(tokens(i), ">")
result(i) = curtokens(0)
Next
getBefore = result
End Function
Function getBack(ByVal inputText As String) As Variant()
Dim tokens() As String
Dim result() As Variant
Dim curtokens() As String
Dim i As Integer
tokens = Split(inputText, ";")
ReDim result(0 To UBound(tokens))
For i = LBound(tokens) To UBound(tokens)
curtokens = Split(tokens(i), ">")
result(i) = curtokens(1)
Next
getBack = result
End Function
创建按钮绑定宏
在Excel中创建一个按钮,并将其与编写的宏进行绑定,以便通过点击按钮来执行邮件发送操作。
一些问题
- Excel不保存宏:每次写完宏代码后,退出重新打开时不进行保存。解决办法是将Excel设置为英文形式。
热门推荐
托尔斯泰的文学巅峰:三大代表作解析
25年CFA考期大盘点,哪个考期更容易?
2025香港大学学期时间安排详解
工会成立的条件
牛油果尽量少吃最好不吃
如何维修瓷砖裂缝?维修瓷砖裂缝时如何选择合适的维修材料?
什么是支撑位和阻力位?
杠杆的原理是什么意思?杠杆原理在投资中的应用有哪些?
网站建设怎么做?2025年最全建站指南(从规划到运营全流程解析)
有色金属的种类及其在工业中的应用
Excel标题位置调整指南:5种实用方法详解
怀孕期间如何控制体重
声音录制不求人!2025 电脑录屏软件声音设置全攻略
金曲歌王丁真,怎么成了年轻人的AI导师?
肠系膜脂膜炎是怎回事?如何治疗
肠系膜脂膜炎需要做什么检查
Excel中取消超级表的三种方法
手脚冰凉是什么原因
逾600只基金重仓持有!比亚迪股价盘中首破400元大关 多只基金获益颇丰
反向代理与正向代理:区别与应用场景
图文总结:正向代理与反向代理
省亲是什么意思?很多人不知道
基础版的音频功放电路详解:从A类到T类功放的工作原理与应用
“天使投资轮的含义”
深度揭秘:选择牙科服务时需要避开的几大坑
芯片测试需要掌握的一些关键技术
科学制定个人健身计划:目标设定、身体评估到饮食搭配的全攻略
语文古代地名怎么写的
辞退员工劳动法有什么规定
签订了无固定期限劳动合同,公司违法辞退要支付多少赔偿金?