highandwild
Registered User.
- Local time
- Today, 21:33
- Joined
- Oct 30, 2009
- Messages
- 435
Hi all
I'm developing an application where I send emails containing monthly statements to members.
If I don't create an Outlook Application object for each email, as in the code below, I often get an "Operation Failed" error message. I cannot trap it as it is an Outlook message.
What ever I change the fnWait to does not seem to matter.
It works fine as it is but do you have any ideas as to what I can do to avoid creating the Outlook object everytime?
I am using the ClickYes program.
Public Function SendBulkEmails(strSQL) As Boolean
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
Do While Not rs.EOF
Set appOutLook = CreateObject("Outlook.Application")
Call SendIndividualEmail(rs!Address, rs!Subject, rs!Msg)
Call fnWait(5)
rs.MoveNext
Set appOutLook = Nothing
Loop
rs.Close
db.Close
End Function
Private Function SendIndividualEmail(strAddress, strSubject, strMessage)
Dim MailOutLook As Outlook.MailItem
Dim x As Integer
On Error GoTo Err_Handler:
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = strAddress
.Subject = strSubject
.HTMLBody = strMessage
.Send
End With
Set MailOutLook = Nothing
Exit Function
Err_Handler:
MsgBox "An error was encountered." & vbCrLf & "The error message is: " & Err.Description & vbCrLf & strMessage
End Function
Public Function fnWait(intNrOfSeconds As Integer)
Dim varStart As Variant
varStart = Timer
Do While Timer < varStart + intNrOfSeconds
Loop
End Function
I'm developing an application where I send emails containing monthly statements to members.
If I don't create an Outlook Application object for each email, as in the code below, I often get an "Operation Failed" error message. I cannot trap it as it is an Outlook message.
What ever I change the fnWait to does not seem to matter.
It works fine as it is but do you have any ideas as to what I can do to avoid creating the Outlook object everytime?
I am using the ClickYes program.
Public Function SendBulkEmails(strSQL) As Boolean
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
Do While Not rs.EOF
Set appOutLook = CreateObject("Outlook.Application")
Call SendIndividualEmail(rs!Address, rs!Subject, rs!Msg)
Call fnWait(5)
rs.MoveNext
Set appOutLook = Nothing
Loop
rs.Close
db.Close
End Function
Private Function SendIndividualEmail(strAddress, strSubject, strMessage)
Dim MailOutLook As Outlook.MailItem
Dim x As Integer
On Error GoTo Err_Handler:
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = strAddress
.Subject = strSubject
.HTMLBody = strMessage
.Send
End With
Set MailOutLook = Nothing
Exit Function
Err_Handler:
MsgBox "An error was encountered." & vbCrLf & "The error message is: " & Err.Description & vbCrLf & strMessage
End Function
Public Function fnWait(intNrOfSeconds As Integer)
Dim varStart As Variant
varStart = Timer
Do While Timer < varStart + intNrOfSeconds
Loop
End Function