vba - 对象的属性(应为邮件项)生成“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
解决方案
感谢蒂姆威廉姆斯帮助我。我想我可以通过检查它是否是 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
推荐阅读
- c# - 从 C# 中的 xml 文件中读取内容
- mediator - 如何制作具有依赖关系的 wso2 企业集成器自定义中介
- angular - 角加载微调器错误:ExpressionChangedAfterItHasBeenCheckedError
- aws-lambda - Cloudformation - “AWS_PROXY”目前仅支持 Lambda 函数
- visual-studio-code - PlantUML 在类图中不能很好地渲染箭头(Skinparam 线型正交模式)
- go - Golang Facebook 对 struct 的响应
- java - 可以从 Arcgis 运行时 SDK Java 中的地图获取经度纬度吗?
- javascript - 使用 HTML 和 JavaScript 进行日期时间本地输入类型操作
- c# - Selenium - 未正确检查断言
- modx - 我可以在 modx evo 中使用 {{sometitle}} 作为文本(不解析为块)吗?