I'm trying to create a VBA that counts the number of mail items by category in Outlook. I found one already done but when I ran the code I got the error message that aitem and aKey were not defined so I defined them both as objects. Now when I run the code, the message box pops up, but it is empty. No information is listed.
I'm trying to figure out why the data won't display and also if I can get the data to display in something other than a message box, like maybe an excel file.
I've listed the code below:
I'm trying to figure out why the data won't display and also if I can get the data to display in something other than a message box, like maybe an excel file.
I've listed the code below:
VBA Code:
Sub CategoriesEmails()
Dim oFolder As MAPIFolder
Dim oDict As Object
Dim sStartDate As String
Dim sEndDate As String
Dim oItems As Outlook.Items
Dim aitem As Object
Dim sStr As String
Dim sMsg As String
Dim aKey As Object
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