如何引用项目来统计收件箱中的邮件数量?

发布于 2024-11-13 22:52:20 字数 898 浏览 3 评论 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

A small error is preventing this counting code from operating.

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

The error message:

Run-time error '424':
Object required

The debugger highlights the line:

For Each MapiItem In MapiFolderInbox.Messages

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-11-20 22:52:20

好的,这是一个工作版本:

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

Ok, here's a working version:

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
翻了热茶 2024-11-20 22:52:20

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

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

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

另请参阅:选项显式打开

Edit: -- it seems you wish to count Spam messages, judging by the last string in your VBA code. Going with @sitnik's solution seems the better approach.

MapiFolderInbox seems to be undefined -- try assigning it to your Inbox folder, something like:

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

Also see: Option Explicit On

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文