Hi everyone,
I found the base code very useful however the outlook error made the code unusable; here is a modified version which does not result in an error:
Public Function SendNewEMail()
'qryQueryName = the name of the query you want to send e-mails from
'x = the column # of the field with e-mail address
'y = the column # of field with invoice number
'a,b,c = the colum # of fields if you want the e-mail body to have more information from the query (if not/more, you can delete/add as appropriate)
Dim OutApp As Object
Dim OutMail As Object
Dim MyDb As DAO.Database
Dim rsEmail As DAO.Recordset
Set MyDb = CurrentDb()
Set rsEmail = MyDb.OpenRecordset("qryQueryName", dbOpenSnapshot)
With rsEmail
.MoveFirst
Do Until rsEmail.EOF
If IsNull(.Fields(x)) = False Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = rsEmail.Fields(x)
.Subject = "" & rsEmail.Fields
.HTMLBody = "Email Body Text " & vbCrLf & _
"Field A: " & .Fields(a) & vbCrLf & _
"Field B: " & .Fields(b) & vbCrLf & _
"Field C: " & .Fields(c)
'.display
.send
End With
End If
.MoveNext
Loop
End With
Set MyDb = Nothing
Set rsEmail = Nothing
End Function
I found the base code very useful however the outlook error made the code unusable; here is a modified version which does not result in an error:
Public Function SendNewEMail()
'qryQueryName = the name of the query you want to send e-mails from
'x = the column # of the field with e-mail address
'y = the column # of field with invoice number
'a,b,c = the colum # of fields if you want the e-mail body to have more information from the query (if not/more, you can delete/add as appropriate)
Dim OutApp As Object
Dim OutMail As Object
Dim MyDb As DAO.Database
Dim rsEmail As DAO.Recordset
Set MyDb = CurrentDb()
Set rsEmail = MyDb.OpenRecordset("qryQueryName", dbOpenSnapshot)
With rsEmail
.MoveFirst
Do Until rsEmail.EOF
If IsNull(.Fields(x)) = False Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = rsEmail.Fields(x)
.Subject = "" & rsEmail.Fields

.HTMLBody = "Email Body Text " & vbCrLf & _
"Field A: " & .Fields(a) & vbCrLf & _
"Field B: " & .Fields(b) & vbCrLf & _
"Field C: " & .Fields(c)
'.display
.send
End With
End If
.MoveNext
Loop
End With
Set MyDb = Nothing
Set rsEmail = Nothing
End Function