按接收日期获取消息数

发布于 2024-11-10 08:37:46 字数 911 浏览 3 评论 0原文

我有以下代码用于计算 Outlook 中的电子邮件总数:

Sub Test()

    Dim objOutlook As Object, objnSpace As Object, objFolder As Object
    Dim EmailCount As Integer

    Set objOutlook = CreateObject("Outlook.Application")

    Set objnSpace = objOutlook.GetNamespace("MAPI")

    On Error Resume Next

    Set objFolder = objnSpace.Folders("MyFolder").Folders("Spam")

    If Err.Number <> 0 Then
        Err.Clear
        MsgBox "No such folder."
        Exit Sub
    End If

    EmailCount = objFolder.Items.Count
    Set objFolder = Nothing
    Set objnSpace = Nothing
    Set objOutlook = Nothing

    MsgBox "Number of emails in the folder: " & EmailCount, , "Number of spam messages "

End Sub

有人可以给我一些方法来计算一周中的某一天吗?

示例:

  • “周一收到的邮件数 = 476”
  • “周日收到的邮件数量= 121”

或者甚至对于特定日期

  • “11 月 2 日收到的电子邮件数量2010”

I have the following code for counting gross number of e-mails in Outlook:

Sub Test()

    Dim objOutlook As Object, objnSpace As Object, objFolder As Object
    Dim EmailCount As Integer

    Set objOutlook = CreateObject("Outlook.Application")

    Set objnSpace = objOutlook.GetNamespace("MAPI")

    On Error Resume Next

    Set objFolder = objnSpace.Folders("MyFolder").Folders("Spam")

    If Err.Number <> 0 Then
        Err.Clear
        MsgBox "No such folder."
        Exit Sub
    End If

    EmailCount = objFolder.Items.Count
    Set objFolder = Nothing
    Set objnSpace = Nothing
    Set objOutlook = Nothing

    MsgBox "Number of emails in the folder: " & EmailCount, , "Number of spam messages "

End Sub

Could someone please give me some way to count for a particular day of the week?

Examples:

  • "NUMBER OF MESSAGES RECEIVED ON MONDAYS = 476"
  • "NUMBER OF MESSAGES RECEIVED ON SUNDAYS = 121"

Or even for a particular date

  • "NUMBER OF EMAILS RECEIVED ON THE 2nd November 2010"

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

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

发布评论

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

评论(3

始于初秋 2024-11-17 08:37:46

也许将参数(可选 dteDate As Date)添加到整个方法/函数并在主体中包含以下代码

Select Case Weekday(dteDate)
  Case vbMonday  
    IsCorrectDay = True
  Case Else  
    IsCorrectDay = False
End Select

For Each MapiItem In MapiFolderInbox.Messages
  If MapiItem.TimeReceived = IsCorrectDay Then count = count + 1     
Next MapiItem 

Perhaps add argument (Optional dteDate As Date) to whole method/function and contain the following code within the body

Select Case Weekday(dteDate)
  Case vbMonday  
    IsCorrectDay = True
  Case Else  
    IsCorrectDay = False
End Select

For Each MapiItem In MapiFolderInbox.Messages
  If MapiItem.TimeReceived = IsCorrectDay Then count = count + 1     
Next MapiItem 
相权↑美人 2024-11-17 08:37:46

看起来您必须查看每条消息并使用 TimeReceived 属性。

For Each MapiItem In MapiFolderInbox.Messages
            If MapiItem.TimeReceived = dte Then count = count + 1
        Next MapiItem

或类似的东西。

Looks like you will have to look at each message and use the TimeReceived property.

For Each MapiItem In MapiFolderInbox.Messages
            If MapiItem.TimeReceived = dte Then count = count + 1
        Next MapiItem

or something like that.

策马西风 2024-11-17 08:37:46

嗯,这就是我所拥有的。似乎没有任何简单的属性或方法可以为您提供所需的答案。我不知道是否有任何方法可以通过 SQL 查询引用邮件项目。如果有的话,那将是理想的方法。由于这似乎不可能,因此您唯一的选择是迭代文件夹中的所有项目,并为一周中的几天设置几个计数器变量。然后为每个项目增加适当的计数器。

主要问题只是弄清楚如何引用适当的对象。

在 MS Outlook 2003 中进行了测试,似乎可以正常工作。但是 - 我还没有检查它是否正在计算收件箱每个子文件夹中的每个项目,或者它是否只是给出收件箱本身中的项目计数,而不是收件箱子文件夹中的任何项目。
不管怎样,她是:

Sub CountEmailsByDayOfWeek()
 'count mail items by day-of-week
 'Isaac Comer, 6-3-2011

 Dim EmCtSun, EmCtMon, EmCtTue, EmCtWed, EmCtThu, EmCtFri, EmCtSat As Long

 Dim myInbox As Variant
 Dim BUNCH_OF_Items As Items
 Dim myInboxItem As Variant

 Set myInbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
 Set BUNCH_OF_Items = myInbox.Items

 ' < Handle any errors like No such folder here >

 ' clear counters for each day-of-week 
 ' "EmCt" means "Email Count". It wasn't a good choice of variable name 
 ' because some "Items" aren't "Emails", but I'm not fixing it right now
 EmCtSun = 0: EmCtMon = 0: EmCtTue = 0: EmCtWed = 0: EmCtThu = 0: EmCtFri = 0: EmCtSat = 0


' disabled test case with the "first" item in Inbox--testing to make sure I had the object reference correct
'     Set myInboxItem = BUNCH_OF_Items(1)
'          With myInboxItem
'               Debug.Print Chr(34) & .Subject & Chr(34) & " Received: "; .ReceivedTime
'               Debug.Print DatePart("w", .ReceivedTime)
'          End With

 For Each myInboxItem In BUNCH_OF_Items
      With myInboxItem
           'disabled code that shows the subject line and received time
           'Debug.Print Chr(34) & .Subject & Chr(34) & " Received: "; .ReceivedTime
           Select Case DatePart("w", .ReceivedTime) ' get the numeric day-of-week corresponding to the vb constants
                Case vbSunday
                     EmCtSun = EmCtSun + 1
                Case vbMonday
                     EmCtMon = EmCtMon + 1
                Case vbTuesday
                     EmCtTue = EmCtTue + 1
                Case vbWednesday
                     EmCtWed = EmCtWed + 1
                Case vbThursday
                     EmCtThu = EmCtThu + 1
                Case vbFriday
                     EmCtFri = EmCtFri + 1
                Case vbSaturday
                     EmCtSat = EmCtSat + 1
           End Select
      End With
 Next

 ' output
 ' send output both to a msgbox and the immediate window.
 Debug.Print "Counts for Emails received Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday respectively."
 Debug.Print EmCtSun; EmCtMon; EmCtTue; EmCtWed; EmCtThu; EmCtFri; EmCtSat
 MsgBox _
      "Sunday:" & vbTab & vbTab & EmCtSun & vbCrLf & _
      "Monday:" & vbTab & vbTab & EmCtMon & vbCrLf & _
      "Tuesday:" & vbTab & vbTab & EmCtTue & vbCrLf & _
      "Wednesday:" & vbTab & EmCtWed & vbCrLf & _
      "Thursday:" & vbTab & EmCtThu & vbCrLf & _
      "Friday:" & vbTab & vbTab & EmCtFri & vbCrLf & _
      "Saturday:" & vbTab & EmCtSat, _
      vbOKOnly + vbInformation, "Count of Items By Day-of-week Received"
End Sub

如果这个代码有帮助,请投票给我。我需要积分。 :-)

Well this is what I've got. There does not appear to be any simple property or method to give you the answer you're wanting. I don't know if there's any way to reference the mail items through a SQL query. If there were, that would be the ideal approach. Since that doesn't seem to be possible, you're only option is to iterate over ALL the items in the folder (or folders) and have several counter variables for the days of the week. Then you increment the appropriate counter for each item.

The main problem is simply figuring out how to reference the appropriate object.

Tested in MS Outlook 2003 and appears to be working. BUT--I haven't check to see if it's counting every item in every sub-folder of Inbox or if it's just giving a count of items in Inbox itself, but not any of the items in sub-folders of the Inbox.
Anyway, her it is:

Sub CountEmailsByDayOfWeek()
 'count mail items by day-of-week
 'Isaac Comer, 6-3-2011

 Dim EmCtSun, EmCtMon, EmCtTue, EmCtWed, EmCtThu, EmCtFri, EmCtSat As Long

 Dim myInbox As Variant
 Dim BUNCH_OF_Items As Items
 Dim myInboxItem As Variant

 Set myInbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
 Set BUNCH_OF_Items = myInbox.Items

 ' < Handle any errors like No such folder here >

 ' clear counters for each day-of-week 
 ' "EmCt" means "Email Count". It wasn't a good choice of variable name 
 ' because some "Items" aren't "Emails", but I'm not fixing it right now
 EmCtSun = 0: EmCtMon = 0: EmCtTue = 0: EmCtWed = 0: EmCtThu = 0: EmCtFri = 0: EmCtSat = 0


' disabled test case with the "first" item in Inbox--testing to make sure I had the object reference correct
'     Set myInboxItem = BUNCH_OF_Items(1)
'          With myInboxItem
'               Debug.Print Chr(34) & .Subject & Chr(34) & " Received: "; .ReceivedTime
'               Debug.Print DatePart("w", .ReceivedTime)
'          End With

 For Each myInboxItem In BUNCH_OF_Items
      With myInboxItem
           'disabled code that shows the subject line and received time
           'Debug.Print Chr(34) & .Subject & Chr(34) & " Received: "; .ReceivedTime
           Select Case DatePart("w", .ReceivedTime) ' get the numeric day-of-week corresponding to the vb constants
                Case vbSunday
                     EmCtSun = EmCtSun + 1
                Case vbMonday
                     EmCtMon = EmCtMon + 1
                Case vbTuesday
                     EmCtTue = EmCtTue + 1
                Case vbWednesday
                     EmCtWed = EmCtWed + 1
                Case vbThursday
                     EmCtThu = EmCtThu + 1
                Case vbFriday
                     EmCtFri = EmCtFri + 1
                Case vbSaturday
                     EmCtSat = EmCtSat + 1
           End Select
      End With
 Next

 ' output
 ' send output both to a msgbox and the immediate window.
 Debug.Print "Counts for Emails received Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday respectively."
 Debug.Print EmCtSun; EmCtMon; EmCtTue; EmCtWed; EmCtThu; EmCtFri; EmCtSat
 MsgBox _
      "Sunday:" & vbTab & vbTab & EmCtSun & vbCrLf & _
      "Monday:" & vbTab & vbTab & EmCtMon & vbCrLf & _
      "Tuesday:" & vbTab & vbTab & EmCtTue & vbCrLf & _
      "Wednesday:" & vbTab & EmCtWed & vbCrLf & _
      "Thursday:" & vbTab & EmCtThu & vbCrLf & _
      "Friday:" & vbTab & vbTab & EmCtFri & vbCrLf & _
      "Saturday:" & vbTab & EmCtSat, _
      vbOKOnly + vbInformation, "Count of Items By Day-of-week Received"
End Sub

If this code was helpful please vote for me. I need points. :-)

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