I have a MS Access application that sends email via outlook automation using vba code found here: Create an email with Outlook-Automation. I am using the code almost entirely unaltered under the heading 'Create an email with Outlook-Automation'.
After a while (a couple of days, maybe 100 emails to 1000 recipients), Outlook will hang, and thus pause all other code my Access application should be running. The message Outlooks displays (usually 3-5 of these boxes) is below.
I have tried 'killing' the outlook application before trying to send every email, but the outlook application is usually closed... and the code opens, sends, and closes again. All objects are closed and set to nothing at the end of code execution.
I am looking for a solution to prevent this from happening. The PC is not out of resources, is updated and plenty powerful. I am running 64bit Office 365. Restarting the PC on a set schedule may work, but the PC hosts an SQL Server used my others 24/7 and the restart will interrupt their work.
EDIT: Slightly altered code below - in vba, although I could not get it to format correctly without using javascript snippet. The EmailApp() function checks to see if Outlook is open or closed, so the app knows how to initialize the outlook object, and how to leave it at the end.
Public Function funSendEmail()
Dim EmailType As String
Dim myMail As Object
Dim myOutlApp As Object
Const olMailItem = 0
EmailType = EmailApp()
If EmailType = "OutlookOpen" Then
Set myOutlApp = GetObject(, "Outlook.Application")
ElseIf EmailType = "OutlookClosed" Then
Set myOutlApp = CreateObject("Outlook.Application")
End If
Set myMail = myOutlApp.CreateItem(olMailItem)
With myMail
.To = "[email protected]"
.Subject = "Test"
.HTMLBody = "Test Message"
.Send
End With
If EmailType = "OutlookOpen" Then
'Do Nothing
ElseIf EmailType = "OutlookClosed" Then
myOutlApp.Quit
End If
Set myMail = Nothing
Set myOutlApp = Nothing
End Function
