2013-12-16 4 views
0

나는 자동으로 전자 메일을 보내려는 MS Access 2010 db가 있습니다. 쿼리를 설정했지만 CDO VBA에 문제가 있습니다. 그들은 'qryEmails'라고하며 다음의 4 개 가지 필드가 포함되어 있습니다 쿼리 내가 액세스 어떻게합니까CDO 전자 메일 자동화

ReturnCode, SalesOrderNumber, Name, EmailAddress 

: 나열된 각 레코드를 통해

  1. 루프를 각각의 이메일 주소로 이메일을 보내을
  2. 각 이메일에 처음 세 입력란에 대한 참조가 포함될 메시지가 있으므로 각 메시지가 맞춤 설정되어 나타납니다.
  3. 동적 제목이 있으므로 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"" <[email protected]>" 
    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 개의 이메일을 보내고 있는지 알고 싶습니다. 지금까지 쿼리 결과는 테스트 목적으로 두 개의 주소 만 반환합니다.

답변

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 
관련 문제