如何获得发件人的电子邮件地址?

问题描述:

我希望Outlook对来自某个电子邮件地址的电子邮件执行操作.

I want Outlook to perform an action on email from a certain email address.

在ThisOutlookSession中,我有:

In the ThisOutlookSession I have:

Private Sub Application_NewMail() 'This triggers when a new email is recieved
    Call TestSub
End Sub

在一个模块中,我有:

Public Sub TestSub()
    Dim Msg                 As Outlook.MailItem
    Dim FromEmailAddress    As String

    FromEmailAddress = Msg.SenderEmailAddress

    If FromEmailAddress = "Test@example.com" Then
        MsgBox ("Hello")
    End If

End Sub

我知道

运行时错误"91":
未设置对象变量或With块变量

Run-time error '91':
Object variable or With block variable not set

FromEmailAddress = Msg.SenderEmailAddress上.

我尝试过对代码进行多种变体,并用尽了Google的强大力量.

I have tried many variations on my code and exhausted the powers of Google.

您可以使用以下代码:

Dim oInbox    As Outlook.Folder
Dim oItem     As Object
Dim Msg       As MailItem

Set oInbox = ActiveExplorer.Session.DefaultStore.GetRootFolder().Folders("Inbox")
For Each oItem In oInbox.Items
    If TypeOf oItem Is MailItem Then
        Set Msg = oItem
        FromEmailAddress = Msg.SenderEmailAddress
    Else
        Debug.Print "Skipping " & TypeName(oItem)
    End If
Next