[英]CDO Email Automation
I have a MS Access 2010 db that I want to send emails from automatically. 我有一个要自动发送电子邮件的MS Access 2010数据库。 I have the query set up but am getting stuck with the CDO VBA.
我已经设置了查询,但是被CDO VBA卡住了。 They query is called 'qryEmails' and contains the following 4 fields:
他们的查询称为“ qryEmails”,其中包含以下4个字段:
ReturnCode, SalesOrderNumber, Name, EmailAddress
How do I get Access to: 我如何访问:
ReturnCode
field is in each subject ReturnCode
字段 I have been trying small steps at first, so far I am receiving 100's of emails to the same address. 一开始我一直在尝试一些小步骤,到目前为止,我已经收到100封发送到相同地址的电子邮件。 Here is my code (I have used XXX where I do not want to disclose info):
这是我的代码(我在不愿透露信息的地方使用了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
Any idea why this is sending 100's of emails? 知道为什么要发送100封电子邮件吗? The query result so far is only returning two addresses for testing purposes.
到目前为止,查询结果仅返回两个地址以进行测试。
Within the loop, the recordset remains on the same row. 在循环内,记录集保留在同一行上。 And since the recordset row does not change, it never reaches
rst.EOF
并且由于记录集行不会更改,因此它永远不会到达
rst.EOF
That code includes a disabled line for MoveNext
. 该代码包括
MoveNext
的禁用行。 Uncomment that line. 取消注释该行。 And you probably want to position it just before the
Loop
statement. 您可能希望将其放置在
Loop
语句之前。
Do While Not rst.EOF
' do everything you need for current record,
' then move to the next record ...
rst.MoveNext
Loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.