好的 - 这似乎差不多完整;但是一个小错误就是阻止了这个计数代码的运行。
以下是代码:
Sub Count2
Dim objOutlook As Object, objnSpace As Object, objFolder As Object
Dim Count As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objnSpace.Folders("My Personal Emails").Folders("spam")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
For Each MapiItem In MapiFolderInbox.Messages
Select Case Weekday(MapiItem.TimeReceived)
Case vbMonday
Count = Count + 1
End Select
Next MapiItem
MsgBox "Number of spam messages sent on a Monday: " & Count
End Sub
以下是错误消息:
运行时错误'424': 需要对象
调试器突出显示该行:
MapiFolderInbox.Messages中的每个MapiItem
任何帮助将不胜感激!
答案 0 :(得分:1)
编辑: - 您似乎希望计算垃圾邮件消息,根据您的VBA代码中的最后一个字符串来判断。使用@ sitnik的解决方案似乎是更好的方法。
MapiFolderInbox似乎未定义 - 请尝试将其分配到您的收件箱文件夹,例如:
Set MapiFolderInbox = objnSpace.Folders("My Personal Emails").Folders("Inbox")
答案 1 :(得分:1)
好的,这是一个有效的版本:
Sub Count2()
Dim i As Integer, Count As Integer
Dim objOutlook As Outlook.Application
Dim objNSpace As Outlook.NameSpace
Dim objFolder As Outlook.Folder
Set objOutlook = CreateObject("Outlook.Application")
Set objNSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNSpace.Folders("My Personal Emails").Folders("spam")
Count = 0
For i = 1 To objFolder.Items.count
If Weekday(objFolder.Items(i).ReceivedTime) = vbMonday Then
Count = Count + 1
End If
Next i
MsgBox "Number of spam messages sent on a Monday: " & Count
End Sub