0

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?

Vlad
  • 55
  • 1
  • 14

1 Answers1

0

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.

Vlad
  • 55
  • 1
  • 14
  • Can you please show how you refactored the code to get it working? As I have attempted to refactor this code in Python 3.6 and I cannot get it to work with OnAddItem event. – ShatteredPheonix May 05 '18 at 23:15