Outlook + Python

Hi All,

I am tryin to do a personal project where in I get emails for the next couple of hours and then apply regex to the body of the email and then fillout a form with it.

I am able to do the automation part but i am unable to received live emails or emails for the next few hours after i have run the program.

I searched and found similar code to get for the past day or week .I tired tinkering with it a bit but havent gotten any result.

import win32com.client
import os
import time
import datetime as dt

date_time = dt.datetime.now()
#time set to 1 hour
lastHourDateTime = dt.datetime.now() - dt.timedelta(hours = 1)

outlook = win32com.client.Dispatch(“Outlook.Application”).GetNameSpace(“MAPI”)
inbox = outlook.GetDefaultFolder(6)

messages = inbox.Items
messages.Sort("[ReceivedTime]", True)

lastHourMessages = messages.Restrict("[ReceivedTime] >= ‘" +lastHourDateTime.strftime(’%m/%d/%Y %H:%M %p’)+"’")

print ("Current time: "+date_time.strftime(’%m/%d/%Y %H:%M %p’))
print (“Messages from the last hour:”)

for message in lastHourMessages:
print (message.subject)
#print (message.ReceivedTime)

print (“Using GetFirst/GetNext”)
message = lastHourMessages.GetFirst()
while message:
print (message.subject)
message = lastHourMessages.GetNext()

i tried adding like 6 hours to the above code but doesnt seem to work.Would you guys have any idea about this. Any ideas would be appreciated.

Thanks :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.