PDA

View Full Version : emailItem.Send Error Message "Outlook doesn't recognize one or more names"



ScottyBee
01-20-2021, 02:41 PM
Hello

I have an Excel workbook that has a form the user fills out. When finished, the user clicks a Submit button and the underlying code attaches the workbook to an Outlook email. This button worked just fine last week. This week I am getting an error message even though I have not made any changes.

My code is this:


Sub Submit()
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Are you ready to submit?", vbYesNo + vbQuestion + vbDefaultButton2, "Submission Confirmation")
If Answer = vbYes Then
Dim FileName1 As String
FileName1 = Range("A2").Text
ActiveWorkbook.SaveAs Filename:=FileName1
Dim emailApp As Object
Dim emailItem As Object
Set emailApp = CreateObject("Outlook.Application")
Set emailItem = emailApp.CreateItem(0)
'Email Subject
Dim SubTitle As String
SubTitle = Range("A1").Text
Dim selfEmail As Variant
Set selfEmail = Range("F6")

'Email Details
emailItem.to = "JimSmith@xyz.com"
emailItem.CC = selfEmail
emailItem.Subject = SubTitle
emailItem.Body = "See attached interest form."
emailItem.Attachments.Add ActiveWorkbook.FullName
emailItem.Send
VBA.Interaction.MsgBox "Your submission has been received! Thank you.", , "Submission Confirmation"
Set emailItem = Nothing
Set emailApp = Nothing
Else
Exit Sub
End If

End Sub


The error message I get is:
27771

When I click Debug, the following line of code is highlighted:

27772


Any ideas on why this line of code is being flagged? I have added the Outlook 15.0 object library to this project. Thanks

gmayor
01-20-2021, 09:46 PM
If you look at your debug screen shot, the email 'address' at the top of the listed code emailItem.to or the value of 'selfemail' are not valid e-mail addresses. My guess is the latter.

ScottyBee
01-21-2021, 10:35 AM
Hello gmayor, that is all it was. My value for the variable "selfEmail" was invalid. I was looking for a more "complicated" reason why code wasn't working. The error message led me down the wrong path. Simple coding mistake. I appreciate your time. Thanks :)