'Error while sending Outlook email using Excel VBA

I would like, within Excel, to send an email from a userform.

I created test code, according to Microsoft how to page.

When I try to use .send (email address swapped to dummy one for the purpose of the question)

"application-defined or object-defined error".

When I use .display the message shows up on the screen. I have added references for Outlook 16 and Office 16, as suggested in other solutions.

I wonder if it is an issue related to company policies.

Sub Mail_workbook_Outlook()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim oListObj As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    email_to = "[email protected]"
    email_body = "test 123"
    email_subject = "Test email"
    
    With OutMail
        .To = email_to
        .CC = ""
        .BCC = ""
        .Subject = email_subject
        .Body = email_body
        '.Display
        .Send ' THIS LINE IS CAUSING ERROR FOR ME
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub


Solution 1:[1]

The Send method of the MailItem class is a protected method in the Outlook object model which means it can throw security prompts in Outlook or just give errors when using an unsafe Outlook Application instance. It happens when you try to automate Outlook from external applications. You can find a similar thread which lists possible solutions, see How to avoid the Outlook Security warning for email automation - VBA.

There are several ways for suppressing security prompts/errors in the code:

  1. Use a third-party components for suppressing Outlook security warnings. See Security Manager for Microsoft Outlook for more information.

  2. Use a low-level API instead of OOM. Or any other third-party wrappers around that API, for example, Redemption.

  3. Develop a COM add-in which has access to the trusted Application object.

  4. Use group policy objects for setting up machines.

See Outlook Object Model Security Warnings for more information. Also you may find the following articles helpful:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene Astafiev