0

I want to to generate an email through Outlook using Excel VBA.

Then I want to automatically send this email WITHOUT previewing the message/email.

Dim outlookApp As Outlook.Application
Dim outlookMail As Outlook.MailItem

Set outlookApp = New Outlook.Application
Set outlookMail = outlookApp.CreateItem(olMailItem)

With outlookMail
    .to = "email_here"
    .Subject = "Test"
    .Body = "test"
    .Send
End With

Set outlookMail = Nothing
Set outlookApp = Nothing

I did turn on the references through the visual basic toolbar to Outlook.

I get all the way to .Send then the error msg

run-time error '287' Application-define or object-defined error

appears.

Community
  • 1
  • 1
Sebastian
  • 13
  • 7
  • Possible duplicate of [Runtime error 287. Sending Emails through Outlook using VBA in Access.](https://stackoverflow.com/questions/36328068/runtime-error-287-sending-emails-through-outlook-using-vba-in-access) – Cyril Sep 27 '19 at 15:49
  • Note that what i listed as a duplicate is for Access, while you're using Excel... same principle, you need to delay the send so the system has time to generate the email. – Cyril Sep 27 '19 at 15:50
  • Where Should I incorporate application.wait at? – Sebastian Sep 27 '19 at 16:05
  • Application.Wait (Now + TimeValue("00:01:00")) – Sebastian Sep 27 '19 at 16:19
  • I added the above line of code right before '.send' and however I am still getting the same error on .send – Sebastian Sep 27 '19 at 16:20
  • Have you had success generating the email without the send? `.Display` would need to be at the top inside of your with-statement. – Cyril Sep 27 '19 at 16:34
  • Yes if .send is not in the code no errors at all just whenever I add .send – Sebastian Sep 27 '19 at 16:36
  • Can you check to make sure that your email address is formatted correctly? I run many variations of this same exact code, late bind and early binding without errors. Outlook should queue up the creation without any issues. If the email address is incorrect it WILL throw an error. Runtime error if email is not displayed, 287 if outlook is displayed and has an issue. Are you running any other code before or after this that is ALSO using Outlook? Might be a conflict between the functions. – Mike Sep 27 '19 at 16:56
  • Another possible duplicate. If in a corporate environment `.Send` may be disabled. https://stackoverflow.com/questions/48104512/how-to-send-mail-when-the-send-does-not-work and https://stackoverflow.com/questions/17883088/sending-mail-using-outlook-where-the-send-method-fails – niton Sep 27 '19 at 17:59
  • Thank you the sendKeys ending up working in the previous form @Mike, I guess my IT department did have security features in Outlook preventing the .send method to work – Sebastian Sep 27 '19 at 19:15

0 Answers0