1

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.

braX
  • 11,506
  • 5
  • 20
  • 33
BCLtd
  • 1,459
  • 2
  • 18
  • 45
  • 1
    Remove all `On Error …` lines and check if any errors occur. Both lines hide all error messages if errors occur. But if you cannot see them you cannot fix them. That means if the email doesn't get sent you won't even notice it because there is no waring at all. You might benefit from reading [VBA Error Handling – A Complete Guide](https://excelmacromastery.com/vba-error-handling) • Your code reads like *"Try to send an email with Outlook but if it goes wrong don't tell anyone, just act like everything was fine."* – Pᴇʜ Jun 12 '19 at 09:27
  • I commented that out... but its still the same. I thought it may of placed something in the outbox, or draft but nothing. I'll have a try at putting more error handling in to catch the error – BCLtd Jun 12 '19 at 09:31
  • If both `On Error …` lines are not active and you don't get any errors, then the email should be sent or if Outlook is not configured properly stuck in the outbox. The code looks good to me. What server are you using to send the email? SMTP or Exchange? • If you have more than one mail account in Outlook make sure you check them all. – Pᴇʜ Jun 12 '19 at 09:35
  • I was thinking that maybe there is some security setting that I can enable on outlook - it's exchange. The company operate on very tight security, so maybe that could be a reason? – BCLtd Jun 12 '19 at 09:55
  • 1
    Check this https://stackoverflow.com/questions/6122747/send-email-from-excel-in-exchange-environment – Pᴇʜ Jun 12 '19 at 09:58
  • Which line is giving you the error? – 0m3r Jun 12 '19 at 23:31
  • This may be a security setting. This poster had to resort to Sendkeys. https://stackoverflow.com/questions/17883088/sending-mail-using-outlook-where-the-send-method-fails – niton Aug 14 '19 at 11:43

0 Answers0