1

I referenced many websites and even copied and pasted code. I cannot get my Excel macro button to send an email.

When I click RunSub/Userform(play button) in VBAProject I get

runtime error 287

Sub Send_Email()
    Dim MyOutlook As Object
    Set MyOutlook = CreateObject("Outlook.Application")

    Dim MyMail As Object
    Set MyMail = MyOutlook.CreateItem(olMailItem)

    MyMail.To = "notlistedpublicly"
    MyMail.Subject = Range("B6") & "Has completed his Skills Matrix"
    MyMail.Body = Range("B6") & "has completed his Skills Matrix. Please review"

    MyMail.Send
End Sub
Community
  • 1
  • 1
Paul C.
  • 11
  • 2
  • It could be a security related [issue](https://stackoverflow.com/questions/57204574/run-time-error-287-application-defined-or-object-defined-error-while-using) – Storax Aug 10 '22 at 12:47

1 Answers1

0

I have a similar code, I added some lines to from it to your code.

Sub Send_Email()

Dim MyOutlook As Object
Set MyOutlook = CreateObject("Outlook.Application")

Dim MyMail As Object
'I change this
'Set MyMail = MyOutlook.CreateItem(olMailItem)
Set MyMail = MyOutlook.CreateItem(0)

MyOutlook.Session.Logon
With MyMail
      .To = "notlistedpublicly"
      .Subject = Range("B6") & "Has completed his Skills Matrix"
      .Body = Range("B6") & "has completed his Skills Matrix. Please review"
      'Before try to see how the mail looks.
      '.Send
      .Display
End with

Set MyMail = Nothing
Set MyOutlook = Nothing

End Sub
pepefiestas
  • 187
  • 8
  • Alright so I got it to function where it opens the email prepped to be sent, but I want it to automatically send the email after clicking the macro button. I notice that when I put .send in the code that it creates a debug issue. – Paul C. Aug 10 '22 at 14:47
  • Is Outlook open? – pepefiestas Aug 10 '22 at 15:02
  • 1
    Yes. I've checked other forums and some say it could be a security restriction because its on a work laptop. .Display work, but not .send – Paul C. Aug 10 '22 at 15:14