如果主题与字符串匹配,我正在尝试使用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
有人知道为什么我在运行时出现类型不匹配错误?
答案 0 :(得分:14)
如评论所述,请尝试在代码中加入 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
编辑1:根据德米特里的建议,您也可以使用:
If Item.Class = 43 Then
取代
If TypeOf Item Is Outlook.MailItem Then