[英]Returning outlook sender address from VBA in Excel
The following code I have copied from this answer here.我从这里的答案中复制了以下代码。 It triggers the myItems_ItemAdd when a new email is received and is run from within excel.当收到新的 email 并从 excel 内运行时,它会触发 myItems_ItemAdd。 (Note I am not able to call New Mail events directly from outlook) (注意我不能直接从 Outlook 调用新邮件事件)
Private WithEvents myItems As Outlook.Items
Private Sub Class_Initialize()
Dim oNS As Namespace
Dim myOL As Outlook.Application
Set myOL = New Outlook.Application
Set oNS = myOL.GetNamespace("MAPI")
Set myItems = oNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub myItems_ItemAdd(ByVal Item As Object)
Debug.Print "Got_EMAIL!!!"
End Sub
I then wanted to access various other properties of the email like the subject.然后我想像主题一样访问 email 的各种其他属性。 This for example works:例如,这有效:
Private Sub myItems_ItemAdd(ByVal Item As Object)
Debug.Print "Got_EMAIL!!!"
Debug.Print Item.Subject
End Sub
I would have thought as per the docs that a MailItem (which Item is?) should be accessible with:我会根据文档认为 MailItem (哪个项目是?)应该可以通过以下方式访问:
Debug.Print Item.Sender
But that results in a Run-time error '287'但这会导致运行时错误“287”
I also tried lines:我也试过行:
Debug.Print Item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10130102")
Debug.Print Item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C1F001E")
Debug.Print Item.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
Which has the error:哪个有错误:
When I inspect the Item object I notice that Sender and MAPIObject are both empty.当我检查项目 object 时,我注意到 Sender 和 MAPIObject 都是空的。 I have been unable to find an answer to why Sender properties would be accessed through Property Accessor.我一直无法找到为什么可以通过 Property Accessor 访问 Sender 属性的答案。
How can I access the sender's email address?如何访问发件人的 email 地址?
*** UPDATE ** *** 更新 **
Item.Sender.Address results in Run-time error '287' Item.Sender.Address 导致运行时错误“287”
And as previously stated Sender and MAPIObject are both empty:如前所述, Sender 和 MAPIObject 都是空的:
.Sender
will return an AddressEntry
object (see here ) that cannot be handled by Debug.Print
. .Sender
将返回Debug.Print
无法处理的AddressEntry
object(请参见此处)。 To get the email address as a string use .Sender.Address
.要将 email 地址作为字符串使用.Sender.Address
。
Note: the above gives the expected result when the sender is outside my organization, but not for my colleagues!注意:上面给出了发件人在我的组织之外时的预期结果,但不是我的同事! See here for a possible solution.请参阅此处了解可能的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.