0

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

Error message:

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

MS example: https://learn.microsoft.com/en-us/office/vba/api/Outlook.Recipients

Line that fails: Set MailRecip = App.Recipients.Add("Jon Grande")

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 Smithwick@yahoo.com")
        
        MailRecip.Type = 1    'Designates the above is TO recipients
        .Subject = "5105088005@tmomail.net"
        .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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109

3 Answers3

0

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 Smithwick@yahoo.com")
    MailRecip.Type = 1    'Designates the above is TO recipients
    .Subject = "5105088005@tmomail.net"
    .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
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

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 Smithwick@yahoo.com")
    
    MailRecip.Type = 1    'Designates the above is TO recipients
    .Subject = "5105088005@tmomail.net"
    .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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks, Eugene, I missed the obvious. I applied the correction to Set .Receipients to the Mail Object instead of the App Object, but still missing something, as I am getting the same Err. – Graham Smithwick May 31 '22 at 23:12
0

Got it!

 Set App = CreateObject("Outlook.application")
Dim msgRecipient As String
msgRecipient = "****************@gmail.comt"                                     
Dim oMail As Object                                                         
Set oMail = App.CreateItem(0)
Set MailRecip = oMail.Recipients.Add(msgRecipient)
With oMail
        Set MailRecips = oMail.Recipients
        MailRecip.Type = 1    'Designates the above is TO recipients
        .Subject = Me.[Company]
        .Body = "<a href='tel:1925***-****'> To Call(925-***-*****)</a> "
    If Not MailRecips.ResolveAll Then   
        oMail.Display
    End If
    .Send
    'MailRecip.send    'this sends the mail
End With
Set MailRecip = Nothing
Set oMail = Nothing
Set App = Nothing