[英]Outlook VBA How to loop through inbox and list from email email address if subject matches
I'm trying to use Outlook VBA to loop through the inbox and list the from email address if the subject matches a string. 我正在尝试使用Outlook VBA循环收件箱,如果主题与字符串匹配,则列出来自电子邮件地址。 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? 有人知道为什么我在运行时出现类型不匹配错误?
As commented, try incorporating a test for MailItem in 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:正如德米特里所建议的那样,你也可以使用:
If Item.Class = 43 Then
in place of 代替
If TypeOf Item Is Outlook.MailItem Then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.