I am currently having an issue around automatically sending emails via excel vba? I have a simple example:
Sub sendOutlookEmail()
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "[email protected]"
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End Sub
This works fine when an outlook client is open. When outlook is closed I receive the following error:
Error 287: Application-defined or object-defined error
Now if I add a single line of inconsequential code above the oMail.Send Command:
Debug.Print oApp.Session.Accounts.Item(1).DisplayName
Now the code works fine regardless of Outlook open or closed.
I am aware that Windows Authentication is required to auto send an email via the OLE when Outlook is closed and is using LDAP, but as the extra code is unchanging of the sub routine. I believe this is some sort of bug around the initialisation of the Outlook.application.Session.Account default object.
Could you please help is this an LDAP issue or a Outlook.Application class issue, This is a replicated error from a VB app that I do not have access to the source code for, but is behaving in the same way and reporting the same error.
Thank You