VeryForgetful
Board Regular
- Joined
- Mar 1, 2015
- Messages
- 242
Hi,
I use the code below to count emails in my inbox with a total count for each date.
The problem is that some of the dates come out incorrectly formatted and some show as in the future. Can someone have a look at my code please to help me identify the problem?
I use the code below to count emails in my inbox with a total count for each date.
The problem is that some of the dates come out incorrectly formatted and some show as in the future. Can someone have a look at my code please to help me identify the problem?
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
Application.ScreenUpdating = False
Set ns = Outlook.GetNamespace("MAPI")
On Error Resume Next
Set f = ns.Folders("Personal Folders").Folders("Inbox")
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
ActiveSheet.Rows(FirstRow & ":" & ActiveSheet.Rows.Count).Clear
ActiveSheet.UsedRange.Borders.LineStyle = xlNone
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 dates that have emails
For Each o In dict.Keys
NextRow = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row + 1
msg = o
ActiveSheet.Range("C" & NextRow) = msg
Next
' Output email count per day:
For Each o In dict.Keys
NextRow = ActiveSheet.Range("D" & Rows.Count).End(xlUp).Row + 1
msg = dict(o)
ActiveSheet.Range("D" & NextRow) = msg
Next
Range("C1").CurrentRegion.Sort key1:=Range("C1"), order1:=xlDescending, Header:=xlYes
Application.ScreenUpdating = False
End Sub
Function GetDate(dt As Date) As String
GetDate = Int(dt)
End Function
[IMG]http://i65.tinypic.com/b4vvk1.png[/IMG]<strike></strike>
Thanks