Outlook中的类别计数[关闭]

问题描述 投票:-2回答:1

我是VBA的新手,我正在尝试计算Outlook文件夹中不同类别的电子邮件数量。

我的Outlook文件夹有10个类别(类别1,类别2,类别3 ...)。我想在Excel工作表上获取每个类别中的电子邮件数量。

输入:要计算电子邮件的日期范围

输出(在excel文件中):电子邮件总数:第1类:第2类:未分类:

根据我的初步研究,我可以找到以下代码:

Option Explicit

Private Sub HowManyEmails()

    Dim objFolder As Folder
    Dim EmailCount As Integer

    Dim myItem As Object
    Dim o As Variant

    Dim dateStr As String
    Dim myItems As Items
    Dim dict As Object
    Dim msg As String
    Dim oDate As String

    On Error Resume Next
    Set objFolder = ActiveExplorer.CurrentFolder

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

    ' Must closely follow an On Error Resume Next
    On Error GoTo 0

    EmailCount = objFolder.Items.Count
    MsgBox "Number of emails in the folder: " & EmailCount, , "email count"

    Set dict = CreateObject("Scripting.Dictionary")

    ' oDate = InputBox("Date for count (Format D-M-YYYY")
    oDate = InputBox("Date for count (Format YYYY-m-d")

    Set myItems = objFolder.Items.Restrict("[Received] >= '" & oDate & "'")

    ' myItems.SetColumns ("Categories") ' You will find this error due to On Error GoTo 0

    For Each myItem In myItems
        dateStr = myItem.Categories
        If Not dict.exists(dateStr) Then
            dict(dateStr) = 0
        End If
        dict(dateStr) = CLng(dict(dateStr)) + 1
    Next myItem

    msg = ""
    For Each o In dict.Keys
        If o = "" Then
            msg = msg & dict(o) & ":   " & "Not categorized" & vbCrLf
        Else
            msg = msg & dict(o) & ":   " & o & vbCrLf
        End If
    Next
    MsgBox msg

ExitRoutine:
    Set objFolder = Nothing
    Set dict = Nothing

End Sub

要求您根据要求调整以上代码,并在所需的excel中获得输出

vba outlook
1个回答
0
投票

当然,您可以使用Find / FindNextRestrict方法来查找与您的条件相对应的项目。但是这些方法可以一次在单个文件夹中搜索项目。因此,我建议使用AdvancedSearch类的Application方法,它具有以下优点:

  • 搜索是在另一个线程中执行的。您不需要手动运行另一个线程,因为AdvancedSearch方法会在后台自动运行它。

  • 可以在任何位置(即超出某个文件夹的范围)搜索任何项目类型:邮件,约会,日历,便笺等。可以将RestrictFind / FindNext方法应用于特定的Items集合(请参阅Outlook中Items类的Folder属性)。

  • 完全支持DASL查询(自定义属性也可用于搜索)。您可以在MSDN的Filtering文章中了解有关此内容的更多信息。为了提高搜索性能,如果为商店启用了“即时搜索”,则可以使用“即时搜索”关键字(请参阅IsInstantSearchEnabled类的Store属性)。

  • 您可以随时使用Stop类的Search方法停止搜索过程。

在您的示例代码中,Received属性用于过滤电子邮件。但是您确实对Categories属性感兴趣。

© www.soinside.com 2019 - 2024. All rights reserved.