Home > other >  Outlook mail restriction on current date
Outlook mail restriction on current date

Time:07-03

I am doing automation on the manual work where I am reading the data from outlook mail using win32.

But I need to read mail restrict on the current date otherwise won't read the mail. If mail is not there on a current date then it will skip this part. Below are the codes that compare the received date and today but getting the error.

outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")

folder = outlook.GetDefaultFolder(6)

messages = folder.Items
messages.Sort("[ReceivedTime]", True)
CurrentDateTime = dt.datetime.now()
CurrentDateTime = CurrentDateTime.strftime('%m/%d/%Y %H:%M %p')
messages = messages.Restrict("[ReceivedTime] == '"   CurrentDateTime  "'")
print(messages)
for m in messages:
    if m.Subject.startswith('Daily SSC count to SD Team'):
        m1=m.Body
        print(m1)
        break;

Error:

File "<COMObject <unknown>>", line 2, in Restrict
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot parse condition. Error at "=".', None, 0, -2147352567), None)

CodePudding user response:

It should be only one = not two == so try >= Items.Restrict method (Outlook)

Example

messages = messages.Restrict("[ReceivedTime] >= '"   CurrentDateTime  "'")

also update your strftime to strftime('%m/%d/%Y')

CodePudding user response:

Never use = when working with date/time properties - the condition will never be satisfied even if the value (both date and time) matches that of an existing message because of round-off errors.

Always use a range (< and >), or (if you are dealing with the current date and assume there won't be any messages with a newer date), use >= as @0m3r suggested.

  • Related