locked
Using VBA to .Send an Outlook email gives error 287 RRS feed

  • Question

  • I send automatic emails with VBA macros. We recently switched to Outlook as our email system and we're trying to get the emails to work again.  The examples I'm looking at online fail when .send -ing the email.  The workaround is to .display and sendkeys s, but this is a last-resort hack that I don't want in the code long-term.

    What is the correct way to send emails under these circumstances?  I'm assuming .send is right but it's failing due to some security settings.  If that's so, what settings and where?

    Sub Sample()
       'Setting up the Excel variables.
       Dim olApp As Object
       Dim olMailItm As Object
       Dim iCounter As Integer
       Dim Dest As Variant
       Dim SDest As String

       'Create the Outlook application and the empty email.
       Set olApp = CreateObject("Outlook.Application")
       Set olMailItm = olApp.CreateItem(0)

        With olMailItm
            .To = "person name"
            .Subject = "Test"
            .Body = "No sendkeys"
            .Send 'Error 287
        End With

       'Clean up the Outlook application.
       Set olMailItm = Nothing
       Set olApp = Nothing
    End Sub

    Tuesday, May 22, 2018 2:06 PM

All replies

  • Hello Krausr79,

    Is the Outlook opened while running the code? Have you tried to run the code both while Outlook opened or closed? Will both of them fail to run?

    Besides, what's the version of your office? If you update to latest version, will the issue still occur?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 23, 2018 3:00 AM
  • We have Office 365 ProPlus with excel version 1802, build 9029.2253 click-to-run.  We also run these sorts of macros from Access.  We always have Outlook open.  I tried it with outlook closed and the result was the same.  A task icon appeared in the lower right saying 'Outlook is being used by another program' and then disappeared after I stopped debugging.

    I don't have control over which version/update of Office we have.

    Wednesday, May 23, 2018 1:25 PM
  • Hello Krausr79,

    Did you installed antivirus software on your computer? If you go to File->Options->Trust Center->Trust Center Settings->Programmatic Access, what's the setting in this page? Do you have the same setting as below screenshots?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 24, 2018 7:13 AM
  • My options look like this.  I don't see anything that looks like a Programmatic Access option or Antivirus status anywhere in options.  Have I given you wrong information?  We use Excel 2016, but on the File>Account page the Product, version, and build are as given.

    Trusted Publishers
    Trusted Locations
    Trusted Documents
    Trusted Add-in Catalogs
    Add-ins
    ActiveX Settings
    Macro Settings
    Protected View
    Message Bar
    External Content
    File Block Settings
    Privacy Options

    Thursday, May 24, 2018 1:27 PM
  • Hello Krausr79,

    Sorry for my careless, the setting is in Outlook. Please try to check the setting in your Outlook.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 25, 2018 1:50 AM
  • There it is.  Antivirus Status is valid, and all radio buttons are blank.  So what would I beg IT to do to get it working?
    Friday, May 25, 2018 1:58 PM
  • Hello Krausr79,

    I think it maybe related to Outlook security guard. Please try to refer to below link to check if it is helpful for you.

    Outlook security guard

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 28, 2018 8:43 AM