mdmilner I used some code that you supplied on a previous post. I've modified it. I need loop through my recordset and send an email for each record in the database. I have 2 test records in my database. The first one sends no problem (although I get the message asking if I really want to email. I'd like to get rid of it)
When the code loops, it picks up the second record, but fails on:
olMail.To = strEmailAddress
Sub SetRecipients()
Dim olApp As Object, olMail As Object
'Dim rngeAddresses As Range, rngeCell As Range
Dim strRecipients As String
Dim strEmailAddress As String
Dim dbUserID As Double
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set dbs = CurrentDb()
strSQL = "SELECT * FROM tblContact"
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailitem)
Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
Do Until rs.EOF
strRecipients = .Fields(2).Value
strEmailAddress = .Fields(1).Value
dbUserID = .Fields(0).Value
'format e-mail
olMail.To = strEmailAddress
'olMail.Attachments.Add ReportName
olMail.Subject = Date & " Report"
olMail.Body = strRecipients & " Here is your UserID: " & strUserID
olMail.DeleteAfterSubmit = True
olMail.Send
.MoveNext
Loop
End With
End Sub
Thanks for any help you can provide
Kathy
When the code loops, it picks up the second record, but fails on:
olMail.To = strEmailAddress
Sub SetRecipients()
Dim olApp As Object, olMail As Object
'Dim rngeAddresses As Range, rngeCell As Range
Dim strRecipients As String
Dim strEmailAddress As String
Dim dbUserID As Double
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set dbs = CurrentDb()
strSQL = "SELECT * FROM tblContact"
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailitem)
Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
Do Until rs.EOF
strRecipients = .Fields(2).Value
strEmailAddress = .Fields(1).Value
dbUserID = .Fields(0).Value
'format e-mail
olMail.To = strEmailAddress
'olMail.Attachments.Add ReportName
olMail.Subject = Date & " Report"
olMail.Body = strRecipients & " Here is your UserID: " & strUserID
olMail.DeleteAfterSubmit = True
olMail.Send
.MoveNext
Loop
End With
End Sub
Thanks for any help you can provide
Kathy