0

Here is a tiny sample from my Outlook summary, that actually has 941 incoming emails from 50 people (names, etc., changed to protect the guilty!).

Screenshot

From Subject Received Size
Veronica Topic A 15/10/2025 32 KB
Graham Topic B Tue 14:25 32 KB
Geirge Topic D 30/10/2025 33 KB
Betty Topic W Wed 03/12 38 KB
Robert Topic F 01/04/2024 42 KB
Yvette Topic J 19/02/2024 99 KB

I can sort the data by name, topic, or size—but NOT by date!

I cannot find a way to change that format in Outlook, so I have tried to create an Excel "function"—but nothing I have tried works :-(

Public Function DateFromOutlook(myDate As String) As String
    ' MicroSoft's Outlook gives the date of each file,
    ' but the format differes with the age of the file !
    ' 1 Old files - dd/mm/yyyy
    ' 2 This week - ddd dd/mm
    ' 3 Today - ddd hh:mm
    ' =IF(ISNUMBER(LEFT(C129,1)),C129,DATE(YEAR(TODAY()),MID(C129,5,2),RIGHT(C129,2)))
    
    Dim myYear, myMonth, myDay
    
    If IsADigit(Left(myDate, 1)) Then ' 1 Old files - dd/mm/yyyy
        DateFromOutlook = myDate
    ElseIf HowManyOfThese(myDate, "/") = 1 Then ' 2 This week - ddd dd/mm
        myDay = WorksheetFunction.TODAY()
        myYear = WorksheetFunction.Year(myDay)
        myMonth = Right(myDate, 2)
        myDay = Mid(myDate, 5, 2)
        'DateFromOutlook=DATE(YEAR(TODAY()),MID(mydate,5,2),RIGHT(mydate,2)))
    ElseIf HowManyOfThese(myDate, ":") = 1 Then ' 3 Today - ddd hh:mm
        DateFromOutlook = WorksheetFunction.TODAY()
    End If

End Function
5
  • 1
    So what is the exact problem? Excel cannot sort by date because it does not recognize your values as dates? How is your worksheet created? Commented Dec 12 at 22:21
  • Please edit you post and explain what problem or error you're experiencing with your code. Commented Dec 12 at 22:22
  • "my Outlook summary" - how exactly is that generated? Commented Dec 13 at 3:33
  • Tim - I open Outlook, and select a folder from the list shown in the panel on the left. Then I click in the top panel on the right, that shown a list if emails in that folder. Then I press Ctrl A to select everything, Ctrl C copies it, then I click on a blank sheet in Excel, and press Ctrl V to paste it all. Commented Dec 15 at 0:02
  • @RobinClay you may consider to get the emails (Subject, ReceivedTime, ...) of a folder by a macro. So no conversion of these different date formats is necessary. Commented Dec 15 at 14:50

2 Answers 2

2

Try like this maybe:

Public Function DateFromOutlook(myDate As String) As Date 'return an actual date...
    Dim dtpart As String, arr
    
    If myDate Like "#*/#*/####" Then ' 1 Old files - dd/mm/yyyy
        arr = Split(myDate, "/")
        DateFromOutlook = DateSerial(arr(2), arr(1), arr(0))
    ElseIf HowManyOfThese(myDate, "/") = 1 Then ' 2 This week - ddd dd/mm
        dtpart = Split(myDate, " ")(1) 'drop the day name
        DateFromOutlook = DateSerial(Year(Date), Right(dtpart, 2), Left(dtpart, 2))
    ElseIf HowManyOfThese(myDate, ":") = 1 Then ' 3 Today - ddd hh:mm
        DateFromOutlook = Date 'use today's date
    Else
        DateFromOutlook = CVErr(xlErrNA) 'catch-all for unmatched formats
    End If
End Function

Function HowManyOfThese(strVal, c As String) As Long
    HowManyOfThese = Len(strVal) - Len(Replace(strVal, c, ""))
End Function

EDIT: this is not much more code, doesn't need any date re-processing, and will handle subfolders. Needs a VBA reference to the outlook library.

Sub Main()
    Const PROCESS_SUBFOLDERS As Boolean = True
    Dim myolApp As Outlook.Application, objNS As Outlook.Namespace
    Dim objFolder As Outlook.Folder
    Set myolApp = GetObject(, "Outlook.Application") 'outlook must be open...
    Dim c As Range
    
    Set objNS = myolApp.GetNamespace("MAPI")
    MsgBox "Select the folder you want to create a listin for", _
            vbInformation

    'The listing in Excel will begin at cell `c`
    Set c = Selection '<< or use some specific location
    
    Set objFolder = objNS.PickFolder  'select the folder to list out
    If Not objFolder Is Nothing Then
        ProcessFolder objFolder, c, PROCESS_SUBFOLDERS, objFolder.Name
        MsgBox "Transfer Complete!"
    Else
        MsgBox "Outlook folder selection cancelled!"
    End If
    Application.StatusBar = False
End Sub

Sub ProcessFolder(StartFolder As Outlook.MAPIFolder, c As Range, _
                  doSubFolders As Boolean, Optional sfPath As String = "")
    Dim objItem As Object, objFolder As Outlook.Folder
        
    Application.StatusBar = "Processing " & StartFolder
    
    ' process all the items in this folder
    For Each objItem In StartFolder.Items
        If TypeName(objItem) = "MailItem" Then
            c.Value = objItem.Sender
            c.Offset(0, 1).Value = objItem.ReceivedTime
            c.Offset(0, 2).Value = objItem.Subject
            c.Offset(0, 3).Value = sfPath
            c.Offset(0, 4).Value = Round(objItem.Size / 1024, 2) & "kb"
            Set c = c.Offset(1)    'new row down
        End If
    Next
    
    ' process any subfolders of this folder?
    If doSubFolders Then
        For Each objFolder In StartFolder.Folders
            sfPath = sfPath & "/" & objFolder.Name 'append to the path
            ProcessFolder objFolder, c, doSubFolders, (sfPath) 'sfPath is passed by value
        Next
    End If
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, Tim ! I added 2 more comments but posted them in repty to Ron's suggestion below !
1

This can also be accomplished using Power Query, available in Windows Excel 2010+ and Microsoft 365 (Windows or Mac). Power Query can interpret all of those date strings as dates except for the time.

To use Power Query

  • Select some cell in your Data Table
  • Data => Get&Transform => from Table/Range
  • When the PQ Editor opens: Home => Advanced Editor
  • Make note of the Table Name in Line 2
  • Paste the M Code below in place of what you see
  • Change the Table name in line 2 back to what was generated originally.
  • Read the comments and explore the Applied Steps to understand the algorithm
let

//Change Table Name to your actual table name
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],

//Set the data types
//PQ will interpret the information as dates except for the entry that is only a Time
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"From", type text}, {"Subject", type text}, {"Received", type date}, {"Size", type text}},"en-150"),

//The "time" entry returns an error, so we replace it with today's date
    #"Replace Error" = Table.ReplaceErrorValues(#"Changed Type",{"Received", Date.From(DateTime.FixedLocalNow())})
in
    #"Replace Error"

enter image description here

6 Comments

Thank you, Tim ! After 45 years of programing you have shown me things I have never seen before !
But Alas ! I have JUST discovered that there is yet another type, when the date is e,g, "Wed 03/12", so "this month, rather that the "Tue 18:54" for this week. [ Maybe this is yet another "too clever by half" downgrade ! ]
Didn't power query handle that properly?
Sorry ! Those were intended for Tom !
Thank you, Ron ! I had never heard of "Power Query" ! But as I am now 86, I fear that would be "a whole new ball game" !
Well you are only four years older than me. So still young enough. Take a look when you have a chance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.