Search code examples
pythonoutlookmapi

Python Outlook extract mails by Date Range gives a fixed lower limit, irrespective of User Input


This is a bit hard to explain but let me try !!!

I tried reading Outlook Mails using Python (MAPI) and every time I give a Date Range with the code:

.Restrict("[ReceivedTime] >= '" + Start_user_input.strftime('%Y-%m-%d') + "' AND [ReceivedTime] <= '" + End_user_input.strftime('%Y-%m-%d') + "'"

For Example range is : All mails from 2019 to 2020.

But the output is surprisingly, from 25/06/2019 to whatever the upper user limit is. No matter what, lower limit I give, it always throws up mails from 25/06/2019. And today it is throwing up mails from 26/06/2019.

Hence, it might be some internal setting which makes it output mails from exactly one year ago.

There is no resource on Google for this exact issue, hence stackOverflow is the last hope.

(Also, for example: Range from 2019/01/01 to 2019/06/26 gives me mails for only 26th.

I've tried this on other Outlook accounts but the result is identical.)


Solution

  • To retrieve all Outlook items from the folder that meets the predefined condition, you need to sort the items in ascending order:

    Imports System.Text
    Imports System.Diagnostics
    ' ...
    Private Sub RestrictCalendarItems(folder As Outlook.MAPIFolder)
        Dim dtEnd As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, _
                                             DateTime.Now.Day, 23, 59, 0, 0)
        Dim restrictCriteria As String = "[Start]<=""" + dtEnd.ToString("g") + """" + _
                                         " AND [End]>=""" + DateTime.Now.ToString("g") + """"
        Dim strBuilder As StringBuilder = Nothing
        Dim folderItems As Outlook.Items = Nothing
        Dim resultItems As Outlook.Items = Nothing
        Dim appItem As Outlook._AppointmentItem = Nothing
        Dim counter As Integer = 0
        Dim item As Object = Nothing
       
            strBuilder = New StringBuilder()
            folderItems = folder.Items
            folderItems.IncludeRecurrences = True
            folderItems.Sort("[Start]")
            resultItems = folderItems.Restrict(restrictCriteria)
            item = resultItems.GetFirst()
            Do
                If Not IsNothing(item) Then
                    If (TypeOf (item) Is Outlook._AppointmentItem) Then
                        counter = counter + 1
                        appItem = item
                        strBuilder.AppendLine("#" + counter.ToString() + _
                                              " Start: " + appItem.Start.ToString() + _
                                              " Subject: " + appItem.Subject + _
                                              " Location: " + appItem.Location)
                    End If
                    Marshal.ReleaseComObject(item)
                    item = resultItems.GetNext()
                End If
            Loop Until IsNothing(item)
            If (strBuilder.Length > 0) Then
                Debug.WriteLine(strBuilder.ToString())
            Else
                Debug.WriteLine("There is no match in the " _
                                 + folder.Name + " folder.")
            End If   
    End Sub
    

    In your case, you need to call the folderItems.Sort("[ReceivedTime]") method.