0

一个小错误阻止此计数代码运行。

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”:需要对象

调试器突出显示该行:

For Each MapiItem In MapiFolderInbox.Messages
4

2 回答 2

1

编辑: - 似乎您希望计算垃圾邮件,根据您的 VBA 代码中的最后一个字符串来判断。使用@sitnik 的解决方案似乎是更好的方法。

MapiFolderInbox 似乎未定义 - 尝试将其分配给您的收件箱文件夹,例如:

 Set MapiFolderInbox = objnSpace.Folders("My Personal Emails").Folders("Inbox")

另请参阅:选项显式打开

于 2011-06-07T15:04:37.820 回答
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
于 2011-06-07T16:50:10.120 回答