Sending letters from Excel VBA - names are not recognized

I use the code below to send an email from excel using Outlook:

Private Sub SendEmail()

  Set OutlookApp = CreateObject("Outlook.Application")
  Set OlObjects = OutlookApp.GetNamespace("MAPI")
  Set newmsg = OutlookApp.CreateItem(olMailItem)

  newmsg.Recipients.Add ("name@domain.com; name2@domain.com; name3@domain.com")

  newmsg.Subject = "Test Mail"

  newmsg.Body = "This is a test email."

  'newmsg.Display

  newmsg.Send

End Sub

The code works very well, however I get the following error from Outlook when trying to send an email:

ErrorScreen http://im58.gulfup.com/GRENlB.png

It is strange that if I leave a new message open for two or three minutes, the names will be automatically resolved:

Work http://im74.gulfup.com/qmOYGQ.png

However, this does not suit me, since I do not want the message to be displayed before it is sent. I am looking for it to send as soon as I run the code.

Any suggestions or workarounds would be appreciated.

: " " Outlook, , .

UPDATE:

:

Private Sub SendEmail()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OlObjects = OutApp.GetNamespace("MAPI")
    Set OutMail = OutApp.CreateItem(olMailItem)

On Error Resume Next
    With OutMail
        .To = ("name@domain.com; name2@domain.com; name3@domain.com")
        .Subject = "Test Mail"
        .Body = "This is a test email."
        '.Display
        .Send
    End With

End Sub
+4
2

.Add - "name@domain.com; name2@domain.com; name3@domain.com". Recipients.Add 3 To - .

+7

ResolveAll, . .

:

Sub CheckRecipients()      
 Dim MyItem As Outlook.MailItem 
 Dim myRecipients As Outlook.Recipients 
 Dim myRecipient As Outlook.Recipient 

 Set myItem = Application.CreateItem(olMailItem)      
 Set myRecipients = myItem.Recipients 

 myRecipients.Add("Aaron Con")      
 myRecipients.Add("Nate Sun")      
 myRecipients.Add("Dan Wilson") 

 If Not myRecipients.ResolveAll Then      
     For Each myRecipient In myRecipients      
        If Not myRecipient.Resolved Then      
           MsgBox myRecipient.Name      
        End If      
     Next      
 End If      
End Sub

.

+1

Source: https://habr.com/ru/post/1547248/


All Articles