Outlook VBA 如果主题匹配,如何循环浏览收件箱和电子邮件地址列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24321752/
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 18:21:21  来源:igfitidea点击:

Outlook VBA How to loop through inbox and list from email email address if subject matches

vbaoutlook

提问by Superdooperhero

I'm trying to use Outlook VBA to loop through the inbox and list the from email address if the subject matches a string. Got this so far from googling, but it's not working:

如果主题与字符串匹配,我正在尝试使用 Outlook VBA 循环浏览收件箱并列出发件人电子邮件地址。到目前为止从谷歌搜索中得到了这个,但它不起作用:

Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

Dim oFolder As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
For Each oMail In Items
    Debug.Print oMail.SenderEmailAddress
Next

Anybody know why I get a Type Mismatch error when I run this?

有人知道为什么我在运行它时会收到类型不匹配错误吗?

回答by L42

As commented, try incorporating a test for MailItemin your code:

正如所评论的,尝试在您的代码中加入对MailItem的测试:

Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Dim Item As Object

For Each Item In olFolder.Items
    If TypeOf Item Is Outlook.MailItem Then 
        Dim oMail As Outlook.MailItem: Set oMail = Item
        Debug.Print oMail.SenderEmailAddress
    End If
Next

Edit1:As suggested by Dmitry, you can also use:

编辑 1:根据 Dmitry 的建议,您还可以使用:

If Item.Class = 43 Then

in place of

代替

If TypeOf Item Is Outlook.MailItem Then