初学者在这里
我正在尝试在Outlook中创建一个VBA宏,该宏允许用户选择一个文件夹,然后显示一个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
我希望有人能帮助我。 =)
答案 0 :(得分:0)
感谢Tim Williams帮助我。我认为可以通过检查是否为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