Outlook VBA如果主题匹配,如何在收件箱中循环并从电子邮件地址中列出 [英] Outlook VBA How to loop through inbox and list from email email address if subject matches

查看:506
本文介绍了Outlook VBA如果主题匹配,如何在收件箱中循环并从电子邮件地址中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Outlook VBA遍历收件箱,并在主题匹配字符串时列出发件人的电子邮件地址.到目前为止,它没有进行谷歌搜索,但无法正常工作:

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:

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?

推荐答案

如所评论的,尝试在代码中并入针对 MailItem 的测试:

As commented, try incorporating a test for MailItem in your code:

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 :如Dmitry所建议,您还可以使用:

As suggested by Dmitry, you can also use:

If Item.Class = 43 Then

代替

If TypeOf Item Is Outlook.MailItem Then

这篇关于Outlook VBA如果主题匹配,如何在收件箱中循环并从电子邮件地址中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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