'AttributeError: <unknown>.Senton error in python - Outlook
I'm a complete beginner in python. I want to write a code in python which download a specific file from an specific email (which contains like 3 or 4 excel files) from outlook and stores it in my directory (specific path). I got a code and tryied to modify it to my needs but i got this "AttributeError: .Senton" error. Really appretiate your help or any help.
I tried using win32com.client module because is from an outlook application and saving all the files from today. Here is my code
# -*- coding: latin-1 -*-
import win32com.client
import os
import datetime
today = datetime.date.today()
path = os.path("D:\my_path")
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") #Opens Microsoft Outlook
inbox = outlook.GetDefaultFolder(6) #N4 Invocie folder
messages = inbox.Items #Get first email
def saveattachemnts(subject = "Title Maíl - *"):
for message in messages:
if message.Subject == subject and message.Unread or message.Senton.date() == today:
attachments = message.Attachments
attachment = attachments.Item(1)
for attachment in message.Attachments:
attachment.SaveAsFile(os.path.join(path, str(attachment)))
if message.Subject == subject and message.Unread:
message.Unread = False
break
saveattachemnts()
os.system("this_python.py")
I want this code to run everyday and it has to download the files from an specific mail called "Title Mail - 20190819" and the last date changes each day so I used wildcards like "Title Maíl - *" (with accent) to look up for the rigth email, and in that email it has to download a specific excel file by name (example the name has to be "AB - Consolidado 20190819") and stores it in a directory in my computer, and when finished I curious about if it's possible to at the end run another python whith name "this_python.py" for example, that program splits the excel and saves its files in csv. (this is already done) But rigth now I get this "AttributeError: .Senton " error and I couldn't find much documentation of this.
Solution 1:[1]
I just edit it and now I can use it for each day with a time variable :) so I just get rid of "Setondate" for now it works:
import win32com.client
import os.path
import datetime
a = str((datetime.date.today()).strftime('%Y%m%d'))
def saveattachemnts(subject,name,path):
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") #Opens Microsoft Outlook
inbox = outlook.GetDefaultFolder("6") #N4 Invocie folder
messages = inbox.Items #Get emails
today = datetime.date.today()
pathToSave = os.path.expanduser(path)
for msg in messages:
if msg.Subject == subject and msg.Unread:
break
for att in msg.Attachments:
if att.FileName == name:
if msg.Subject == subject and msg.Unread:
msg.Unread = False
break
att.SaveASFile(pathToSave + "\\" + att.FileName)
print("Mail Successfully Extracted")
saveattachemnts("The subject" + a +"-1",
"The file name - " + a + "-1.xlsx", "D:\my_path")
Solution 2:[2]
Firstly, you are assuming that there are only MailItem
objects in the folder. You can also have ReportItem
, MeetingItem
, etc. which do not expose the SentOn
property. You need to check first that the Class
property (exposed by all OOM objects) == 43
(which is olMail
) - take a look at OOM objects with OutlookSpy (I am its author - click Item button).
Secondly, you are using the ==
operator when checking the SentOn
property. The comparison will never evaluate to true - all date/time properties in COM are doubles: the int part stores the number of days since 12/31/1899 and the fractional part is the time of the day. You will always have round-off errors. You need to use use a range (> and <) or truncate both parts to ints first and then compare them.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yes C. |
Solution 2 |