我有一个数据列表,比如说客户信息(姓名、电子邮件、欠款金额等),存储在 Excel 工作表中。我的目标是单击 Excel 中的按钮,然后将 Outlook 模板中的信息发送给每个客户。
- 创建邮件对象
- 将邮件对象设置为模板文件
- 设置然后用有关当前客户端的数据填充模板 - 大部分都停留在此处,不确定如何在模板中指定变量,然后在 VBA 中与它们关联
- 保存到草稿以供稍后查看/发送
例如亲爱的 << 客户姓名 >> = 亲爱的约翰·史密斯
到目前为止我的代码:
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 格式:
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 上测试成功
关于excel - 使用 Outlook 模板和设置变量从 Excel 2007 VBA 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469960/