如何获得发件人的电子邮件地址? [英] How to get a sender's email address?

查看:162
本文介绍了如何获得发件人的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望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

这篇关于如何获得发件人的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆