locked
Error 287: Application-defined or object-defined error on Mail.item RRS feed

  • Question

  • Hello All,

    I am trying to create a simple macro in Excel to use Outlook to send emails. I am using the  code below however every time I reach the .send command I get the error "Error 287: Application-defined or object-defined"

    Can you please help me?

    Thanks!

    Sub sendEmail()

       On Error GoTo SubError

       Dim olApp As Outlook.Application
       Dim olMailItm As Outlook.MailItem

       Set olApp = CreateObject("Outlook.Application")

       Set olMailItm = olApp.CreateItem(0)

        With olMailItm
            .To = "mail@mail.com"
            .Subject = "test"
            .Body = "test"
            .Send 'this command causes the error
        End With

    SubExit:
        On Error Resume Next
            Set olApp = Nothing
            Set olMailItm = Nothing
            Exit Sub

    SubError:
        Application.StatusBar = ""
        MsgBox "Error: " & vbCrLf & Err.Number & " = " & Err.Description
        Resume SubExit
    End Sub

                  
    Thursday, February 18, 2016 8:35 PM

Answers

  • >>>I am trying to create a simple macro in Excel to use Outlook to send emails. I am using the  code below however every time I reach the .send command I get the error "Error 287: Application-defined or object-defined"

    According to your description, I have reproduced this issue, this issue is caused by that you missed adding reference to the Outlook object library.
    Use the Reference command on the Visual Basic for Applications (VBA) Tools menu to set a reference to Microsoft Outlook xx.x Object Library, where xx.x represents the version of Outlook that you are working with.

    For more information, click here to refer about Automating Outlook from a Visual Basic Application

    • Proposed as answer by David_JunFeng Monday, February 22, 2016 1:22 AM
    • Marked as answer by David_JunFeng Sunday, February 28, 2016 2:26 PM
    Friday, February 19, 2016 3:27 AM

All replies

  • Hi. I don't use Excel VBA, but can you step through the code like you can in Access? If so, have you stepped through your code and verified the olApp and olMailItm are being created properly? Just my 2 cents...
    Thursday, February 18, 2016 8:56 PM
  • >>>I am trying to create a simple macro in Excel to use Outlook to send emails. I am using the  code below however every time I reach the .send command I get the error "Error 287: Application-defined or object-defined"

    According to your description, I have reproduced this issue, this issue is caused by that you missed adding reference to the Outlook object library.
    Use the Reference command on the Visual Basic for Applications (VBA) Tools menu to set a reference to Microsoft Outlook xx.x Object Library, where xx.x represents the version of Outlook that you are working with.

    For more information, click here to refer about Automating Outlook from a Visual Basic Application

    • Proposed as answer by David_JunFeng Monday, February 22, 2016 1:22 AM
    • Marked as answer by David_JunFeng Sunday, February 28, 2016 2:26 PM
    Friday, February 19, 2016 3:27 AM