vba 获取发件人电子邮件地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19970390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:12:02  来源:igfitidea点击:

Get sender email address

vbaemailoutlookoutlook-vba

提问by scb998

I have the following VBA code which is meant to send an automatic email when a specific subject is received.

我有以下 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

I'm getting a compile/object error at the following line:

我在以下行收到编译/对象错误:

strSenderName = Sender.GetExchangeUser().PrimarySmtpAddress

the sender name comes up as "empty".

发件人姓名显示为“空”。

How I can extract the sender's email address?

如何提取发件人的电子邮件地址?

回答by Dave

Originally answered in comments.

最初在评论中回答。

Why not msg.SenderEmailAddress

为什么不是 msg.SenderEmailAddress

回答by alexo

Above accepted answer did not work for me. Instead, I'm using this (works with MeetingItems too):

以上接受的答案对我不起作用。相反,我正在使用它(也适用于 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

Thanks to David Lee for the solution in this thread.

感谢 David Lee 在此线程中提供的解决方案。