如何在Outlook中计算和导出数量和类别名称

时间:2019-07-11 14:10:05

标签: excel vba outlook

我有一些代码,可以让您输入日期范围,以获取所需的信息。我的问题是将数据导出到excel的下一步。

我已经尝试过VBA在excel上运行,但无法获得所需的结果。不幸的是,我对Outlook中的VBA不熟悉

Sub CategoriesEmails()

Dim oFolder As MAPIFolder
Dim oDict As Object
Dim sStartDate As String
Dim sEndDate As String
Dim oItems As Outlook.items
Dim sStr As String
Dim sMsg As String


On Error Resume Next
Set oFolder = Application.ActiveExplorer.CurrentFolder

Set oDict = CreateObject("Scripting.Dictionary")

sStartDate = InputBox("Type the start date (format MM/DD/YYYY)")
sEndDate = InputBox("Type the end date (format MM/DD/YYYY)")

Set oItems = oFolder.items.Restrict("[Received] >= '" & sStartDate & "' And [Received] <= '" & sEndDate & "'")
oItems.SetColumns ("Categories")

For Each aitem In oItems
sStr = aitem.Categories
If Not oDict.exists(sStr) Then
oDict(sStr) = 0
End If
oDict(sStr) = CLng(oDict(sStr)) + 1
Next aitem

sMsg = ""
For Each aKey In oDict.keys
sMsg = sMsg & aKey & ":   " & oDict(aKey) & vbCrLf
Next
MsgBox sMsg

Set oFolder = Nothing

 End Sub

我希望此信息将在消息框中显示,此代码即可完成。

1 个答案:

答案 0 :(得分:0)

尽管日期和时间通常以Date格式存储,但是FindRestrict方法要求将日期和时间转换为字符串表示形式。要确保日期格式符合Microsoft Outlook的预期格式,请使用Format函数。

例如,您可能在那里找到类似的帖子-Outlook Items Restrict with Date issue