Excel macro to send an email generates runtime error 287

Viewed 64

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
1 Answers

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
Related