Home > database >  MS EXAMPLE FAILS Outlook Automation: Runtime Err 438 for .Recipients.Add() USING https://docs.micros
MS EXAMPLE FAILS Outlook Automation: Runtime Err 438 for .Recipients.Add() USING https://docs.micros

Time:06-01

ISSUE:> Outlook Automation fails at [Outlook Application].Recipients.Add ("Jon Grande")

(CREDITS:> A call-out for source credits for vba development of Outlook Automation to Daniel Pineault and Ron Debruin, among many.)

ERR MSG:> Runtime Error 438: "Object does not support this property or method.

MS EXAMPLE FAILS:> https://docs.microsoft.com/en-us/office/vba/api/Outlook.Recipients

CODE APPLYING MS EXAMPLE FAILS:> See ** "Set MailRecip = App.Recipients.Add("Jon Grande")" ** BELOW:

''' Private Sub cmdEmail_Contact_Click()

Call TestOutlookIsOpen                'AUTHOR: Ron Debruin> https://www.rondebruin.nl/win/s1/outlook/openclose.htm
Call GetAppExePath("msaccess.exe")    'AUTHOR:  Daniel Pineault, CARDA Consultants Inc.
' IsAppRunning ("Outlook.Application")    'https://www.devhut.net/createobjectoutlook-application-does-not-work-now-what/
' GetAppExePath("firefox.exe")            'AUTHOR:  Daniel Pineault, CARDA Consultants Inc.
' GetAppExePath ("outlook.exe")           'AUTHOR:  Daniel Pineault, CARDA Consultants Inc.


Dim App                   As Object    'Outlook.Application
Dim Mail                  As Object    'Outlook.MailItem
Dim MailRecip             As Object    'Outlook.Recipient
Const olMailItem = 0

Set App = CreateObject("Outlook.application")

Set Mail = OutlookApp()  '<<<<<<<<<<< See Sub Macro MyMacroThatUseOutlook in TestTheCode Module


With Mail
     
    Set MailRecip = App.Recipients.Add("Jon Grande")
    
     **Set MailRecip = App.Recipients.Add("Jon Grande")**
     Set MailRecip = App.Recipients.Add("Graham [email protected]")
    
    MailRecip.Type = 1    'Designates the above is TO recipients
    .Subject = "[email protected]"
    .Body = "<a href='tel:19254511573'> To Call CaolePepe (925-451-1573)</a> "

    For Each MailRecip In .Recipients
        If Not MailRecip.Resolve Then
            Mail.Display
        End If
    Next

    .Send    'this sends the mail
End With

Set MailRecip = Nothing
Set Mail = Nothing
Set App = Nothing

End Sub '''enter image description here

CodePudding user response:

You are trying to add recipients to the Application object, which makes no sense. Try the updated code below. (off the top of my head):

Call TestOutlookIsOpen                'AUTHOR: Ron Debruin> https://www.rondebruin.nl/win/s1/outlook/openclose.htm
Call GetAppExePath("msaccess.exe")    'AUTHOR:  Daniel Pineault, CARDA Consultants Inc.
' IsAppRunning ("Outlook.Application")    'https://www.devhut.net/createobjectoutlook-application-does-not-work-now-what/
' GetAppExePath("firefox.exe")            'AUTHOR:  Daniel Pineault, CARDA Consultants Inc.
' GetAppExePath ("outlook.exe")           'AUTHOR:  Daniel Pineault, CARDA Consultants Inc.

Dim App                   As Object    'Outlook.Application
Dim Mail                  As Object    'Outlook.MailItem
Dim MailRecip             As Object    'Outlook.Recipient
Const olMailItem = 0

Set App = CreateObject("Outlook.application")

Set Mail = App.CreateItem(0)
With Mail
    Set MailRecip = .Recipients.Add("Jon Grande")
    Set MailRecip = .Recipients.Add("Graham [email protected]")
    MailRecip.Type = 1    'Designates the above is TO recipients
    .Subject = "[email protected]"
    .Body = "<a href='tel:19254511573'> To Call CaolePepe (925-451-1573)</a> "
    For Each MailRecip In .Recipients
        If Not MailRecip.Resolve Then
            Mail.Display
        End If
    Next
    .Send    'this sends the mail
End With

Set MailRecip = Nothing
Set Mail = Nothing
Set App = Nothing

CodePudding user response:

First of all, you need to use the mail object instead of App to be able to call the Recipients property:

With Mail
     
    Set MailRecip = .Recipients.Add("Jon Grande")
    
     **Set MailRecip = .Recipients.Add("Jon Grande")**
     Set MailRecip = .Recipients.Add("Graham [email protected]")
    
    MailRecip.Type = 1    'Designates the above is TO recipients
    .Subject = "[email protected]"
    .Body = "<a href='tel:19254511573'> To Call CaolePepe (925-451-1573)</a> "

    If Not myRecipients.ResolveAll Then 
       Mail.Display
    End If  

    .Send    'this sends the mail
End With

Also there is no need to iterate over all Recipients in the code and checking the Resolve method call results:

 For Each MailRecip In .Recipients
        If Not MailRecip.Resolve Then
            Mail.Display
        End If
    Next

Instead, you could use the Recipients.ResolveAll method which attempts to resolve all the Recipient objects in the Recipients collection against the Address Book.

Read more about that in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.

CodePudding user response:

Sorry, All! Changed the Code to refer the .Recipients property to the Mail object instead of the App object, but still get the same Err 438. Here's the code:

''' Dim App As Object Dim Mail As Object Dim MailRecip As Object Const olMailItem = 0

Set App = CreateObject("Outlook.application")

Set Mail = App.CreateItem(0) 


With Mail
     
     Set MailRecip = App.Recipients.Add("John Grande")
     
     MailRecip.Type = 1    'Designates the above is TO recipients

'''

  • Related