I'm currently trying calculate the leadtime of all outlook messages within a specific outlook folder using python. In general the script runs fine however, after roundabout 2000 emails I received the following error (part of code):
import win32com.client as win32
import pandas as pd
from datetime import date
import time
outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders("[email protected]")
inbox = folder.Folders("Inbox")
messages = inbox.Items
df = pd.DataFrame()
index = 0
for msg in messages:
if (msg.SenderEmailType != "EX"):
print(str(msg.subject))
print('Begin date:', msg.SentOn.strftime('%Y-%m-%d'))
print('End date:',msg.LastModificationTime.strftime('%Y-%m-%d'))
df.at[index, 'Subject'] = msg.subject
df.at[index, 'Begin date'] = msg.SentOn.strftime('%Y-%m-%d')
df.at[index, 'End date'] = msg.LastModificationTime.strftime('%Y-%m-%d')
I encounterd the following error:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\MailCounts.py", line 24, in <module>
print('End date:',msg.LastModificationTime.strftime('%Y-%m-%d'))
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
ValueError: microsecond must be in 0..999999
I tried to debug it by selecting the specific email and put it into another outlook folder and then loop over it. But then it works fine without errors. So it seems to be folder specific?!
I have searched for several answers on the errors and the method in general. But as far as I can see, this answer was never asked before with a suitable answer. Thanks in advance!
This was a bug in PyWin32, reported as issue #1655. It was fixed by pull request #1683, which was merged on 7th of May 2021.
The earliest release after that date is release 301, from the 31st of May 2021. I would suggest upgrading to at least that version, using whatever package manager you happen to be using. Though preferably to the latest stable version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With