VBA Outlook脚本错误:找不到对象

时间:2014-11-22 11:09:44

标签: excel vba excel-vba outlook

我正在尝试使用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

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

我试过以下内容:

If IsNull(olkRcp.AddressEntry) Then
    X400toSMTP = strAdr

但是我仍然在AddressEntry中遇到同样的错误。

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

非常感谢!

1 个答案:

答案 0 :(得分:0)

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