簡體   English   中英

從 Excel 中的 VBA 返回 outlook 發件人地址

[英]Returning outlook sender address from VBA in Excel

我從這里的答案中復制了以下代碼。 當收到新的 email 並從 excel 內運行時,它會觸發 myItems_ItemAdd。 (注意我不能直接從 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

然后我想像主題一樣訪問 email 的各種其他屬性。 例如,這有效:

Private Sub myItems_ItemAdd(ByVal Item As Object)
  Debug.Print "Got_EMAIL!!!"
  Debug.Print Item.Subject     
End Sub

我會根據文檔認為 MailItem (哪個項目是?)應該可以通過以下方式訪問:

Debug.Print Item.Sender

但這會導致運行時錯誤“287”

在此處輸入圖像描述

我也試過行:

 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)

哪個有錯誤:

在此處輸入圖像描述

當我檢查項目 object 時,我注意到 Sender 和 MAPIObject 都是空的。 我一直無法找到為什么可以通過 Property Accessor 訪問 Sender 屬性的答案。

如何訪問發件人的 email 地址?

*** 更新 **

Item.Sender.Address 導致運行時錯誤“287” 在此處輸入圖像描述

如前所述, Sender 和 MAPIObject 都是空的:

在此處輸入圖像描述

.Sender將返回Debug.Print無法處理的AddressEntry object(請參見此處)。 要將 email 地址作為字符串使用.Sender.Address

注意:上面給出了發件人在我的組織之外時的預期結果,但不是我的同事! 請參閱此處了解可能的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM