我有以下VBA代码,用于在收到特定主题时发送自动电子邮件。
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
If item.Class = olMail Then
If Left$(item.Subject, 29) = "Hazard Identification Report" Then
Dim Msg As Outlook.MailItem
Dim NewForward As Outlook.MailItem
Dim myFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Set Msg = item
Set NewForward = Msg.Forward
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
strSender = ""
strSenderName = Sender.GetExchangeUser().PrimarySmtpAddress
If itm.SenderEmailAddress = "EX" Then
Set objSender = itm.Sender
If Not (objSender Is Nothing) Then
Set objExchUser = Sender.GetExchangeUser()
If Not (objExchUser Is Nothing) Then
strSender = objExchUser.PrimarySmtpAddress
End If
End If
Else
strSender = itm.SenderEmailAddress
End If
我在以下行收到编译/对象错误:
strSenderName = Sender.GetExchangeUser().PrimarySmtpAddress
发件人姓名显示为“空”。
如何提取发件人的电子邮件地址?
答案 0 :(得分:9)
最初在评论中回答。
为什么不是msg.SenderEmailAddress
答案 1 :(得分:2)
以上接受的答案对我不起作用。相反,我正在使用它(也可以与MeetingItems一起使用):
Function GetSenderEmail(oM As Variant)
Dim oPA As Outlook.PropertyAccessor
Set oPA = oM.PropertyAccessor
GetSenderEmail = oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
End Function
感谢David Lee在this thread中提供的解决方案。