vba Outlook 无法识别一个或多个名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24638124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:44:53  来源:igfitidea点击:

Outlook does not recognize one or more names

vbaemailoutlook

提问by user3817448

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,

我有以下 vba 代码,它读取邮箱并向任何发送无效代码作为对邮箱的回复的用户发送回复,但有时会收到运行时错误(Outlook 无法识别一个或多个名称)。我的问题是,

  1. Will creation of new MAPI profile resolve the issue or do i need to add a code that resolves the address and ignores if the email id no longer exist. if yes how do i do that?
  2. Also in general whats the parameter to not send emails for specific condition?
  1. 创建新的 MAPI 配置文件会解决问题还是我需要添加一个代码来解析地址并忽略电子邮件 ID 是否不再存在。如果是,我该怎么做?
  2. 另外一般来说,在特定条件下不发送电子邮件的参数是什么?

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

回答by AnnePauline

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.

我曾经遇到过同样的错误,我在代码中疯狂搜索 5 个小时后解决了它。但它要简单得多:1 个电子邮件地址有一个错误,缺少域名中的 .(点)。

回答by Dmitry Streblechenko

Instead of setting the Toproperty, call MailItem.Recipients.Add(returns Recipient object). Call Recipient.Resolve- it will return false if the name cannot be resolved.

而不是设置To属性,调用MailItem.Recipients.Add(返回收件人对象)。调用Recipient.Resolve- 如果无法解析名称,它将返回 false。

回答by Aditya Guru

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.

Item.Recipient.ResolveAll 可用于在发送邮件之前解析名称。如果所有名称都已成功解析,则返回 true。

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)

我已经测试了代码,但没有逐一添加和解析收件人。(由 Dmitry 建议)

I used Item.To, Item.BCC properties. Then used ResolveAll and send the mail only if all names are resolved.

我使用了 Item.To、Item.BCC 属性。然后使用 ResolveAll 并仅在解析所有名称时发送邮件。

回答by Lime D. Zeze

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 "[email protected]" resolved the issue for me.

我刚刚遇到了这个错误;运行多年的代码突然触发了“Outlook 无法识别一个或多个名称”错误。我发现收件人是 Outlook 共享文件夹名称,即。“我的共享文件夹”,无论是 Access 2016 还是 Outlook 问题,该名称都无法再解析为其关联的电子邮件地址。将收件人更改为“[email protected]”为我解决了这个问题。

回答by jac

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.

已解决 在我的 Outlook 案例中,我有多个联系人使用相同的电子邮件(两家公司由一个联系人/电子邮件运营),这造成了问题。我删除了其中一个联系人,因此 Outlook 的联系人列表中没有重复电子邮件,现在它可以工作了。请注意,我的批次仍然向该联系人发送两次电子邮件,其中包含我想要的每家公司的信息。