我有一个MS Access 2010数据库,我想自动发送电子邮件。我已经设置了查询但是卡在了CDO VBA中。它们的查询称为“qryEmails”,包含以下4个字段:
ReturnCode, SalesOrderNumber, Name, EmailAddress
如何获取访问权限:
ReturnCode
字段我一开始就尝试了一些小步骤,到目前为止,我收到100封电子邮件到同一地址。这是我的代码(我使用XXX,我不想透露信息):
Dim rst As ADODB.Recordset
Dim strSQL As String
Dim strEmail As String
Set rst = New ADODB.Recordset
'
strSQL = "[qryEmails]" 'source of recordset
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'
Do While Not rst.EOF
strEmail = rst.Fields("EmailAddress")
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Your refund is:" '
objMessage.FROM = """SENDER"" <XXX@somewhere.com>"
objMessage.To = rst.Fields("EmailAddress")
objMessage.TextBody = objMessage.TextBody & rst(1)
'==Add fields to email body
'Do While strEmail = rst.Fields("EmailAddress")
'rst.MoveNext
'If rst.EOF Then Exit Do
'Loop
' ========= SMTP server configuration
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "XXX"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
'Send email
objMessage.Send
'Clear variable for next loop
Set objMessage = Nothing
Loop
rst.Close
Set rst = Nothing
知道为什么发送100封电子邮件?到目前为止,查询结果只返回两个地址用于测试目的。
答案 0 :(得分:1)
在循环内,记录集保持在同一行。由于记录集行没有改变,因此它永远不会达到rst.EOF
该代码包含MoveNext
的禁用行。取消注释该行。您可能希望在Loop
语句之前定位它。
Do While Not rst.EOF
' do everything you need for current record,
' then move to the next record ...
rst.MoveNext
Loop