Reputation: 39
I have following vba code which reads a mailbox and sends reply to any users who send a invalid code as a reply to the mailbox, but sometimes the run time error (Outlook does not recognize one or more names) is received. My questions are,
Below is the code that we currently have:
Sub ResponseCodeError(Item As Outlook.MailItem)
'If not a valid code then send email to the User
If (Left(Item.Subject, 2) <> "S;" And Left(Item.Subject, 2) <> "N;") Then
Dim outobj, mailobj
Set outobj = CreateObject("Outlook.Application")
Set mailobj = outobj.CreateItem(0)
With mailobj
.To = Item.SenderEmailAddress
.Subject = "Invalid Code"
.Body = "Please use a valid CODE"
.Send
End With
'Move Email to Error Folder
mailboxNameString = "mailboxname"
FolderName = "Error"
Dim olApp As New Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olCurrExplorer As Outlook.Explorer
Dim olCurrSelection As Outlook.Selection
Dim olDestFolder As Outlook.MAPIFolder
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olCurrExplorer = olApp.ActiveExplorer
Set olCurrSelection = olCurrExplorer.Selection
Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(FolderName)
Item.Move olDestFolder
End If
Set outobj = Nothing
Set mailobj = Nothing
End Sub
Upvotes: 3
Views: 31240
Reputation: 66316
Instead of setting the To
property, call MailItem.Recipients.Add
(returns Recipient
object). Call Recipient.Resolve
- it will return false if the name cannot be resolved.
Upvotes: 4
Reputation: 1
SOLVED In my case in Outlook I had several contacts with the same e-mail (two companies that are run by one contact/e-mail) and that created the problem. I deleted one of the contacts so no e-mail is repeated in the contact list in Outlook and now it works. Note that my batch still sends an e-mail twice to that contact which info for each company which is what I wanted it.
Upvotes: 0
Reputation: 11
I just ran into this error; code that had been working for years suddenly triggered the "Outlook does not recognize one or more names" error. I discovered that the recipient was an Outlook shared folder name, ie. "My Shared Folder" and whether it is an Access 2016 or Outlook issue, the name could no longer resolve to its associated email address. Changing the recipient to "mysharedfolder@blahblah.com" resolved the issue for me.
Upvotes: 1
Reputation: 712
The issue might be due to incorrect names, extra characters or spaces in some property of Item.(especially To, BCC, CC or collective property Recipients)
It might also be due to names not resolved yet before sending the mail. I am not sure but would assume that the error was due to trying to resolve names while sending the mail and probably being unable to resolve them due to some issue. Explicitly resolving names like the code below before the mail is sent should solve the issue.
Item.Recipient.ResolveAll can be used to resolve the names before sending the mail. It returns true if all names were successfully resolved.
Code: (Reference)
If Not myRecipients.ResolveAll Then
For Each myRecipient In myRecipients
If Not myRecipient.Resolved Then
MsgBox myRecipient.Name
End If
Next
End If
I have tested the code without adding and resolving recipients 1 by 1.(suggested by Dmitry)
I used Item.To, Item.BCC properties. Then used ResolveAll and send the mail only if all names are resolved.
Upvotes: 1
Reputation: 83
I had once the same error, and I resolved it after 5 hours searching like crazy in the code. But it was much simpler: 1 email address had an error missing the .(dot) in the domain name.
Upvotes: 4