从Excel提取Outllok邮件时出错

问题描述 投票:0回答:1

我需要提取Outlook应用程序中某个文件夹中包含的所有邮件。这是我用来连接文件夹的代码:

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("XXXXXX")

这是我用来提取邮件的循环:

i = 1
For Each MyItem In Folder.Items
    If MyItem.ReceivedTime >= Range("B1").Value Then

        Range("A3").Offset(i, 0).Value = MyItem.Subject
        Range("B3").Offset(i, 0).Value = MyItem.ReceivedTime
        Range("C3").Offset(i, 0).Value = MyItem.SenderName
        Range("D3").Offset(i, 0).Value = MyItem.Body

        i = i + 1

     End If
Next MyItem

代码运行正常,但是现在,我开始收到以下错误:对象不支持此属性或方法。在此行:

If MyItem.ReceivedTime >= Range("B1").Value Then

调试时,看起来MyItem对象当时为空,但如果为true,则循环不应运行。

excel vba outlook-vba
1个回答
0
投票

似乎是MailItem以外的其他项目?类似ReportItem的东西,没有ReceivedTime属性。您可以进行物理检查或使用.Class解决此问题。

尝试

For Each MyItem In Folder.Items
    If MyItem.Class = 43 Then '<~~ olMail
        If MyItem.ReceivedTime < Range("B1").Value Then
            '
            '~~> Reso of your code
            '
        End If
    End If
Next MyItem
© www.soinside.com 2019 - 2024. All rights reserved.