My situation:
I'm trying to send e-mails while Outlook is not open. This code will work if Outlook is running. If Outlook is closed the code will create a non-visible process that you can see running in Task Manager.
The code encounters an error during the .Send
. It returns runtime error 287.
From my experience that are certain VBA methods that will only work when the object is either visible or active.
My work around is to use .Display(False)
before calling .Send
.
After calling .Send
it immediately terminates the Outlook process. (Please point me to the right direction why.) Then the e-mail gets stuck in the Outbox.
I have to apply another work around by calling another CreateObject("Outlook.Application")
and either looping through the Outbox While Outbox.Items.Count > 0
or
Looping through SyncObjects
and manually calling .Start
to run Send/Receive on the Outbox.
My question:
Is there a way to directly use the .Send
method instead of using work arounds while also not having to open Outlook.
My code:
Sub emailer()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub