I am trying to listen for the OnItemAdd event in 2 separate inboxes at the same time with the following code:
class Handler_Class():
def OnItemAdd(self, mail):
#Check if the item is of the MailItem type
if mail.Class==43:
print(mail.Subject, " - ", mail.Parent.FolderPath)
inboxes = ["inbox1", "inbox2"]
for inbox in inboxes:
items = win32com.client.DispatchEx("Outlook.Application").GetNamespace("MAPI").Folders[inbox].Folders["Inbox"].Items
win32com.client.DispatchWithEvents(items, Handler_Class)
print(datetime.now(),"Ready to pump")
pythoncom.PumpMessages()
The pythoncom.PumpMessages() doesn't seem to work though. if I refactor the code to only listen for one inbox's items, it does work.
Any ideas on how to solve this?
This is not possible, and I solved this by initializing all my DispatchWithEvents objects in one thread and capping that up with pythoncom.PumpMessages().
This way you can listen for events such as the ItemAdd in multiple inboxes at the same time.