Michael T
Michael T

Reputation: 1965

Send Outlook email using MS Access VBA

I am trying to send Outlook email using MS Access VBA. I added the Outlook 16.0 reference.

The code returns:

Application-defined of object-defined error.

on

.ReplyRecipients.Add

If I comment out that line, it errors on .Send.

If I run the code in Outlook I get the same error.

Sub TestSend()
    Call SendEmailOutlook("[email protected]", "Test message", "Test message.")
End Sub

Public Sub SendEmailOutlook(strTo As String, strSubject As String, strBody As String)
    
    On Error GoTo SendEmailOutlookErr
    
    Dim strEmail As String
    Dim strMsg As String
    Dim oLook As Object
    Dim oMail As Object
    
    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.createitem(0)
    With oMail
        .ReplyRecipients.Add "[email protected]"
        .to = strTo
        .htmlbody = strBody
        .Subject = strSubject
        .Send
    End With
    Set oMail = Nothing
    Set oLook = Nothing
    Exit Sub
    
SendEmailOutlookErrExit:
        Exit Sub
    
SendEmailOutlookErr:
        MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
        Resume SendEmailOutlookErrExit
End Sub

Upvotes: 1

Views: 252

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

In the code the late binding technology is used, so the COM reference is optional. But if you have already added the Outlook COM refence you may declare all Outlook objects instead of just having the object in the declaration. It can help. Read more about the late and early binding in the Using early binding and late binding in Automation article.

Also the following line of code contains multiple property and method calls:

With oMail
  .ReplyRecipients.Add "[email protected]"

The code is valid. But it makes sense to declare each property or method on a separate line of code, so you could easily find the faulting one - where exactly the error occurs.

The MailItem.ReplyRecipients property returns a Recipients collection that represents all the reply recipient objects for the Outlook item. Use the Add method to create a new Recipient object and add it to the Recipients object. The Type property of a new Recipient object is set to the default for the associated AppointmentItem, JournalItem, MailItem, or TaskItem object and must be reset to indicate another recipient type.

Set myItem = Application.CreateItem(olMailItem)  
Set myRecipient = myItem.Recipients.Add ("Jon Grande")  
myRecipient.Type = olCC

Another aspect is how Outlook has been configured to trust applications on a client computer, an application that uses the Outlook object model to access certain data or execute certain actions can invoke security warnings or throw errors when Outlook is automated without any UI. Depending on the type of information or action that the program was attempting to access or execute, there are three different security prompts that applications can invoke through the Object Model Guard: the address book warning, send message warning, and execute action warning. Read more about that in the Outlook Object Model Security Warnings article.

Upvotes: 1

Related Questions