vba count of Outlook emails, dates in incorrect format

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?

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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Are values from the [SentOn] field 'real' date values or are they text?
 
Upvote 0
Not sure.

I think the GetDate function may be where the error is.

They are either being converted to American format or are being stored as text but cant quite put my finger on the issue.

The cell formatting shows as date so I think they are seen as dates by Excel.

Not sure if I need to incorporate the CDATE function somewhere to stop the format from changing.
 
Upvote 0
Actually, I think that function could be the problem because that is converting the values from the SentOn field to strings which could cause problems later.

If you just want to get the date part from that field try using DateValue/CDate.

Mind you I'm not sure how that would work with the dictionary as you might still need to convert to text, if you do I would suggest you use Format to ensure you keep the correct date format, ie UK.
 
Upvote 0
I've figured it now thanks. See bits in red below.

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
        [COLOR=#ff0000]msg = Format(o, dd - mm - yyyy)[/COLOR]
        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
    [COLOR=#ff0000]GetDate = Day(dt) & "-" & Month(dt) & "-" & Year(dt)[/COLOR]
End Function
 
Upvote 0
Ah, so you did need to use Format as I suggested.:)
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,532
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top