Search code examples
pythonemailoutlookattachmentemail-attachments

Python - outlook not seeing the emails from today


I am trying to download the files/reports that i receive every day from Transportation System.

Already wrote some code (i am just starting with Python so it could not look very professional) but when i am trying to get to the newest emails- program is not able to recognize them.

I checked the len(email) and it showed 2552 emails. When i went to the newest from this "catalog" (2552) it was last email from yesterday (2019-02-15), although today(2019-02-16) i already have an email with an attachment.

Should i modify the syntax as for "email" variable that i am using here ( i guess that this could be an issue that it somehow does not encompass all emails there)?

Will be really grateful for help!

Code is as below:

import win32com.client

folder = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI").GetDefaultFolder(6)

subfolder=folder.Folders(3)

email = subfolder.Items

message = email.Item(2252)

attachment = message.Attachments.Item(1)

attachment.SaveAsFile(r'C:\Users\310295192\Desktop\report.xlsx')

Solution

  • If you never sort the Items collection, it won't be in any particular order. Try to sort it by the ReceivedTime property:

    email.Sort "ReceivedTime", false