【问题标题】:VBA Outlook script error: an object could not be foundVBA Outlook 脚本错误:找不到对象
【发布时间】:2014-11-22 11:09:44
【问题描述】:

我正在尝试使用 VBA 脚本从收件箱中的电子邮件中提取信息到 Excel 电子表格中:

'Subject
'To Address
'From Address
'CC Addresses

对于已经离开组织并且不再在 O365 中的发件人,它会失败。

这是代码位:

Function X400toSMTP(strAdr As String) As String
Dim olkRcp As Outlook.Recipient, olkUsr As Outlook.ExchangeUser
Set olkRcp = Session.CreateRecipient(strAdr)

If olkRcp.AddressEntry = Empty Then
    X400toSMTP = strAdr

ElseIf olkRcp.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
    olkRcp.Resolve
    Set olkUsr = olkRcp.AddressEntry.GetExchangeUser
    X400toSMTP = olkUsr.PrimarySmtpAddress
End If

Set olkRcp = Nothing
Set olkUsr = Nothing
End Function

我运行了调试,它在AddressEntry 停止:The attempted operation failed. An object could not be found 我正在尝试找到一种方法,让脚本为那些在 O365 上找不到的发件人留空地址字段,并进一步处理收件箱中的其余项目。

我尝试了以下方法:

If IsNull(olkRcp.AddressEntry) Then
    X400toSMTP = strAdr

但地址条目仍然出现相同的错误。

我只是一个 VBA 菜鸟,所以非常感谢您的建议。

非常感谢!

【问题讨论】:

  • 你可以写if olkRcp.AddressEntry is nothing then,或者,if isempty(olkRcp.AddressEntry) then ,或者if olkRcp.AddressEntry="" then
  • 非常感谢,帕特里克!我已经尝试过这些,但它仍然给我错误。也许值得一提的是,我在共享邮箱上运行它。我在收件箱中的一个子文件夹上尝试了该脚本,但它并没有在离开者地址处停止,而是在我知道它有效的某个分发列表处停止。我也尝试启用“下载共享文件夹”,但仍然无法用于共享邮箱。

标签: vba excel outlook


【解决方案1】:

确保在访问 AddressEntry 属性之前已解析收件人 - 调用 olkRcp.Resolve。

【讨论】:

  • 我确实试过了,德米特里。但仍然遇到同样的错误
  • 哪种方法会引发该错误?解决?这意味着无法解析名称。如果您在“收件人”框中键入并按 Ctrl+K,是否可以在 Outlook 中解决?
  • 它以某种方式超越了解决方案。它停在 If olkRcp.AddressEntry Is Nothing Then
  • Function X400toSMTP(strAdr As String) As String Dim olkRcp As Outlook.Recipient, olkUsr As Outlook.ExchangeUser Set olkRcp = Session.CreateRecipient(strAdr) olkRcp.Resolve If olkRcp.AddressEntry is nothing then X400toSMTP = strAdr ElseIf olkRcp.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then Set olkUsr = olkRcp.AddressEntry.GetExchangeUser X400toSMTP = olkUsr.PrimarySmtpAddress End If Set olkRcp = Nothing Set olkUsr = Nothing End Function
  • 我使用错误处理使它工作: Function X400toSMTP(strAdr As String) As String Dim olkRcp As Outlook.Recipient, olkUsr As Outlook.ExchangeUser Set olkRcp = Session.CreateRecipient(strAdr) On Error Resume Next If olkRcp.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then olkRcp.Resolve Set olkUsr = olkRcp.AddressEntry.GetExchangeUser X400toSMTP = olkUsr.PrimarySmtpAddress End If Set olkRcp = Nothing Set olkUsr = Nothing On Error GoTo 0 End Function
猜你喜欢
  • 1970-01-01
  • 2018-03-10
  • 2010-12-08
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
相关资源
最近更新 更多