【问题标题】:Counting emails in outlook by date按日期计算 Outlook 中的电子邮件
【发布时间】:2012-02-29 16:09:41
【问题描述】:

我有以下代码来计算 Outlook 文件夹中的电子邮件数量。

Sub HowManyEmails() 
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("Personal Folders").Folders("Inbox").Folders("report's").Folders("Customer")    
    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, , "email count" End Sub

我正在尝试按日期计算此文件夹中的电子邮件,所以我最终每天都会计算一次。

【问题讨论】:

标签: vba outlook ms-office


【解决方案1】:

你可以试试这个代码:

Sub HowManyEmails()

    Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder
    Dim EmailCount As Integer
    Set objOutlook = CreateObject("Outlook.Application")
    Set objnSpace = objOutlook.GetNamespace("MAPI")

        On Error Resume Next
        Set objFolder = objnSpace.Folders("Personal Folders").Folders("Inbox").Folders("report's").Folders("Customer") 
        If Err.Number <> 0 Then
        Err.Clear
        MsgBox "No such folder."
        Exit Sub
        End If

    EmailCount = objFolder.Items.Count

    MsgBox "Number of emails in the folder: " & EmailCount, , "email count"

    Dim dateStr As String
    Dim myItems As Outlook.Items
    Dim dict As Object
    Dim msg As String
    Set dict = CreateObject("Scripting.Dictionary")
    Set myItems = objFolder.Items
    myItems.SetColumns ("SentOn")
    ' Determine date of each message:
    For Each myItem In myItems
        dateStr = GetDate(myItem.SentOn)
        If Not dict.Exists(dateStr) Then
            dict(dateStr) = 0
        End If
        dict(dateStr) = CLng(dict(dateStr)) + 1
    Next myItem

    ' Output counts per day:
    msg = ""
    For Each o In dict.Keys
        msg = msg & o & ": " & dict(o) & " items" & vbCrLf
    Next
    MsgBox msg

    Set objFolder = Nothing
    Set objnSpace = Nothing
    Set objOutlook = Nothing
End Sub

Function GetDate(dt As Date) As String
    GetDate = Year(dt) & "-" & Month(dt) & "-" & Day(dt)
End Function

【讨论】:

  • 感谢您的工作,一个问题是有什么办法可以将此信息保存到 csv 文件中?
  • @fmunkert: +1 做得很好 :) 只有一个建议。不要在MsgBox "No such folder." 之后使用Exit Sub 进行适当的清理:) 记住你仍然在后台运行 Outlook ;)
猜你喜欢
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多