首页 > 解决方案 > 对象的属性(应为邮件项)生成“438”运行时错误:“对象不支持此属性或方法”

问题描述

我正在尝试选择一个文件夹,然后显示一个 MsgBox,其中包含在该文件夹及其所有子文件夹中的设定时间范围内发送的电子邮件数量。

我可以选择一个文件夹,但我得到

'438' 运行时错误:“对象不支持此属性或方法”

在“for”循环之后的行上。

receive_datetime = objCurrentFolder.Items(i).SentOn

这是整个宏:

Sub CountItems()
    Dim lItemsCount As Long
    
    StartDate = DateSerial(2018, 1, 1)
    EndDate = DateSerial(2020, 1, 1)
 
    'Select a folder
    Set objMainFolder = Outlook.Application.Session.PickFolder
 
    If objMainFolder Is Nothing Then
        MsgBox "You choose select a valid folder!", _
          vbExclamation + vbOKOnly, "Warning for Pick Folder"
    Else
        'Initialize the total count
        lItemsCount = 0
        Call LoopFolders(objMainFolder, lItemsCount)
    End If
 
    'Display a message for the total count
    MsgBox "There are " & lItemsCount & " items in the " & _
      objMainFolder.Name & " folder Including its subfolders.", _
      vbInformation, "Count Items"
End Sub

Sub LoopFolders(ByVal objCurrentFolder As Outlook.Folder, lCurrentItemsCount As Long)
    Dim objSubfolder As Outlook.Folder
    Set receiveditems = objCurrentFolder.Items
    
    For i = receiveditems.Count To 1 Step -1
    ' the last item in the collection is your most recent email.
    ' This can be handy to know if your inbox is massive and 
    '  you want to include a Exit For at some point,
    '  e.g. when you run into a date < StartDate

        receive_datetime = objCurrentFolder.Items(i).SentOn
        If receive_datetime >= StartDate And receive_datetime <= EndDate Then
            lCurrentItemsCount = lCurrentItemsCount + 1
        End If
    Next i
 
    'Process all folders and subfolders recursively
    If objCurrentFolder.Folders.Count Then
        For Each objSubfolder In objCurrentFolder.Folders
            Call LoopFolders(objSubfolder, lCurrentItemsCount)
        Next
    End If
End Sub

标签: vbaemailoutlook

解决方案


谢蒂姆威廉姆斯帮助我。我想我可以通过检查它是否是 MailItem 来让它工作。

 Select Case True
        Case TypeOf objCurrentFolder.Items(i) Is Outlook.MailItem
            receive_datetime = objCurrentFolder.Items(i).SentOn
            If receive_datetime >= StartDate And receive_datetime <= EndDate Then
                lCurrentItemsCount = lCurrentItemsCount + 1
            End If
        Case Else
             lCurrentItemsCount = lCurrentItemsCount + 0
    End Select

推荐阅读