VeryForgetful
Board Regular
- Joined
- Mar 1, 2015
- Messages
- 242
Hi,
I have the code below that outputs the email count per day for a generic team inbox, in this example the inbox is named genericqueries.
I am looking for a way to also count the emails in sub folders within this inbox.
Also, this email address contains roughly 8000 emails and it takes an eternity to output the results to a worksheet. Would there be an alternative way to accomplish this quicker than storing the results in a dictionary?
Thanks
I have the code below that outputs the email count per day for a generic team inbox, in this example the inbox is named genericqueries.
I am looking for a way to also count the emails in sub folders within this inbox.
Also, this email address contains roughly 8000 emails and it takes an eternity to output the results to a worksheet. Would there be an alternative way to accomplish this quicker than storing the results in a dictionary?
Thanks
Code:
Sub EmailCount()
Dim ns As Outlook.Namespace
Dim f As Outlook.MAPIFolder
Dim dateStr As String
Dim myItems As Outlook.Items
Dim dict As Object
Dim msg As String
Dim NextRow As Long
Dim FirstRow As Long
Dim SrcSheet As Worksheet
Set SrcSheet = Sheets("Email Summary")
Set ns = Outlook.GetNamespace("MAPI")
On Error Resume Next
Set f = ns.Folders("genericqueries").Folders("Inbox") 'also include sub folders
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder.", vbExclamation
Exit Sub
End If
Set dict = CreateObject("Scripting.Dictionary")
Set myItems = f.Items
myItems.Sort "[SentOn]", True
myItems.SetColumns "[SentOn]"
FirstRow = 2
SrcSheet.Rows(FirstRow & ":" & SrcSheet.Rows.Count).Clear
For Each myItem In myItems
dateStr = Format(myItem.SentOn, "yyyy-mm-dd")
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem
' Output dates that have emails
For Each o In dict.Keys
NextRow = SrcSheet.Range("C" & Rows.Count).End(xlUp).Row + 1
msg = o
Application.StatusBar = msg
SrcSheet.Range("C" & NextRow) = msg
Next
' Output email count per day:
For Each o In dict.Keys
NextRow = SrcSheet.Range("D" & Rows.Count).End(xlUp).Row + 1
msg = dict(o)
Application.StatusBar = msg
SrcSheet.Range("D" & NextRow) = msg
Next
End Sub