vba - Excel VBA - 循环发送网格电子邮件 - 仅发送一封电子邮件
问题描述
我从 stackoverflow 上的其他线程之一获得了此代码。我稍微修改了一下。
这适用于发送一封电子邮件。但是,当我在循环中使用此代码时,它只执行最后一封电子邮件。我之前没有使用过 CDO.Message、CDO.Config 和 Fields。知道为什么会这样吗?
'Part of a procedure
eBody = "html body"
With shTemp
For i = 2 To lastRow
toEmail = "emailAddress"
eAttachment1 = "path1"
eAttachment2 = "path2"
sendEmail = emailPDFs(toEmail, eBody, eAttachment1, eAttachment2)
Next i
End With
'=========
Function emailPDFs(toEmail As String, eBody As String, eAttachment1 As String, eAttachment2 As String)
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
= "smtp.sendgrid.net"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxx"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxx"
.Update
End With
With iMsg
Set .Configuration = iConf
.To = toEmail
.From = "..."
.Subject = "Test email"
.AddAttachment eAttachment1
.AddAttachment eAttachment2
.htmlBody = eBody
.send
End With
End Function
解决方案
推荐阅读
- reactjs - “null”类型的参数不能分配给“RadioGroupState”类型的参数
- r - 为什么 nest() 有时会合并行?
- javascript - 带有索引drawElements的着色器中的WebGL顶点插值,如何在GPU中进行?
- python - 如何使用递归合并排序在每次合并操作后显示列表内容
- python - 我有一个浏览器,我用网络引擎 pyqt 做的,而且它不播放我在 Windows 10 上的实时流
- linux - 如何将 Golang Lambda 函数与 Redis Elasticache AWS 连接
- node.js - NPM:在 Mac OS 上运行安装时出现 libsass 错误
- c - c中的'AND'运算符和'OR'运算符有什么区别
- solidity - 事务和日志顺序
- javascript - Webpack Vue 项目使用 window.Vue 初始化