I am trying to send an email automatically in VBA Excel, however nothing sends. If I change from .Send to .Display it show the correct email and email/to correctly filled in.
The code I am using is
Sub SendEmail()
Dim errorMsg As String
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error GoTo errHandle
With OutMail
.To = "user@email.com"
'.cc = cc
'.bcc = er.emailBcc
.subject = "test"
.htmlBody = "test body"
.Send 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
SendEmailWithOutlook = True
errHandle:
errorMsg = "Error sending mail via outlook: " & Err.Description & vbCrLf
MsgBox errorMsg
SendEmailWithOutlook = False
End Sub
I am receiving the following error
Error sending mail via outlook: Application-defined or object defined error.
Is there anything else I am missing for that to work and send the mail? (it works if I change from .send to .display.