使用 Outlook 模板从 Excel 2007 VBA 发送电子邮件设置变量 [英] Send an email from Excel 2007 VBA using an Outlook Template & Set Variables
本文介绍了使用 Outlook 模板从 Excel 2007 VBA 发送电子邮件设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据列表,比如说客户信息(姓名、电子邮件、欠款等),存储在 Excel 工作表中.我的目标是单击 Excel 中的一个按钮,并在 Outlook 模板中向每个客户发送他们的信息.
I have a list of data, let's say client information (Name, Email, Amount Owing etc.), stored in an Excel worksheet. My aim is to click a button in Excel and send each client their information in an Outlook Template.
- 创建一个邮件对象
- 将邮件对象设置为模板文件
- 设置然后用当前客户端的数据填充模板 - 大部分时间都卡在这里,不确定如何在模板中指定变量,然后在 VBA 中将它们关联起来
- 保存到草稿供以后查看/发送
例如亲爱的<<客户名称 >> = 亲爱的约翰·史密斯
e.g. Dear << clientname >> = Dear John Smith
到目前为止我的代码:
Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:egTemplate.oft")
With MyItem
.To = Worksheets("Clients").Range(1, 2)
.Subject = "Monthly bill"
'Refer to and fill in variable items in template
.Save
End With
Set MyItem = Nothing
Set MyOlApp = Nothing
推荐答案
你可以这样做:
With MyItem
'Refer to and fill in variable items in template
.Body = Replace(.Body, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
或者,如果您的邮件是 HTML 格式:
or, if your mail is in HTML:
With MyItem
'Refer to and fill in variable items in template
.HTMLBody = Replace(.HTMLBody, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
在 Excel/Outlook 2007 上测试成功
Tested successfully on Excel / Outlook 2007
这篇关于使用 Outlook 模板从 Excel 2007 VBA 发送电子邮件设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文