Hi,

A while ago I asked a question[1] about hooking MAPI events in Outlook.
Although this question was answered[2] back then, I am afraid I am still
stuck on the subject: It seems I have been able to hook the AdviseSink,
but the onNotify() never fires. However, the fnevNewMail[3] event is seen
by OutlookSpy[4] in the IMsgStore::Advise() tab on the folder in question,
so I must be doing something wrong.

I have attached my example code, and would appreciate any pointers on the
subject.

Regards,

Arn Vollebregt

[1] http://mail.python.org/pipermail/python-win32/2009-August/009503.html
[2] http://mail.python.org/pipermail/python-win32/2009-August/009504.html
[3] http://msdn.microsoft.com/en-us/library/cc765897.aspx
[4] http://www.dimastr.com/outspy/
from win32com.mapi import mapi
from win32com.mapi.mapitags import *
from win32com.client import gencache
from win32com.server.util import wrap
import time
import pythoncom

# http://msdn.microsoft.com/en-us/library/cc765544.aspx
# http://ftp.twaren.net/local-distfiles/pigfoot/thunderbird/config/WabDefs.h
ulEventType = {
        'fnevCriticalError'                     :0x00000001,
        'fnevNewMail'                           :0x00000002,
        'fnevObjectCreated'                     :0x00000004,
        'fnevObjectDeleted'                     :0x00000008,
        'fnevObjectModified'            :0x00000010,
        'fnevObjectMoved'                       :0x00000020,
        'fnevObjectCopied'                      :0x00000040,
        'fnevSearchComplete'            :0x00000080,
        'fnevTableModified'                     :0x00000100,
        'fnevStatusObjectModified'      :0x00000200,
        'fnevReservedForMapi'           :0x40000000,
        'fnevExtended'                          :0x80000000,
}

class Sink():
        _com_interfaces_ = [mapi.IID_IMAPIAdviseSink]
        _public_methods_ = ['OnNotify']

        # http://msdn.microsoft.com/en-us/library/cc815622.aspx
        def OnNotify(self, cNotif, lpNotifications):
                print "DEBUG: OnNotify()"
                sys.exit()

def main():
        # Outlook 9.0
        #gencache.EnsureModule('{00062FFF-0000-0000-C000-000000000046}', 0, 9, 
0, bForDemand=True)
        #Microsoft Outlook 11.0 Object Library (9.2)
        gencache.EnsureModule('{00062FFF-0000-0000-C000-000000000046}', 0, 9, 
2, bForDemand=True)

        mapi.MAPIInitialize(None) 
        session = mapi.MAPILogonEx(0,"Outlook",None, mapi.MAPI_EXTENDED)
        msgStoresTable = session.GetMsgStoresTable(0)
        propTags = [PR_PROVIDER_DISPLAY_A, PR_DISPLAY_NAME_A, PR_ENTRYID]
        msgStoresRows = mapi.HrQueryAllRows(msgStoresTable, propTags, None, 
None, 0)

        stores = []
        for msgStore in msgStoresRows:
                stores.append((msgStore[1][1],msgStore[2][1]))
        
        for store in stores:
                print ("[%i] %s") % (stores.index(store), store[0])
        choice = int(raw_input("Choose a messageStore: "))
        print ("You have chosen '%s'") % (stores[choice][0])
        
        msgStoreName = stores[choice][0]
        msgStoreID = stores[choice][1]
        
        msgStore = session.OpenMsgStore(0, msgStoreID, None, mapi.MDB_NO_DIALOG 
| mapi.MAPI_BEST_ACCESS)
        hr, data = msgStore.GetProps((PR_ENTRYID, PR_IPM_SUBTREE_ENTRYID), 0)
        store_eid = data[0][1]
        subtree_eid = data[1][1]

        subtreeFolder = msgStore.OpenEntry(subtree_eid, None, 0)
        subtreeFolderHierarchy = subtreeFolder.GetHierarchyTable(0)
        subtreeFolderHierarchyRows = 
mapi.HrQueryAllRows(subtreeFolderHierarchy, None, None, None, 0)

        subfolders = []
        for row in subtreeFolderHierarchyRows:
                subfolders.append(dict(row))

        for subfolder in subfolders:
                print ("[%s] %s") % (subfolders.index(subfolder), 
subfolder[PR_DISPLAY_NAME_A])
        choice = int(raw_input("Choose a subfolder: "))
        print ("You have chosen '%s'") % (subfolders[choice][PR_DISPLAY_NAME_A])
        mailbox = subfolders[choice]
        mailbox_folder = msgStore.OpenEntry(mailbox[PR_ENTRYID], None, 0)
        
        events = ulEventType["fnevNewMail"] | ulEventType["fnevObjectCreated"] 
| ulEventType["fnevObjectModified"]
        sink = session.Advise(mailbox[PR_ENTRYID], events, wrap(Sink()))
        
        try: 
                while(1):
                        time.sleep(1)
        except KeyboardInterrupt:
                session.Unadvise(sink)
                mailbox_folder = None
                subtreeFolderHierarchy = None
                subtreeFolder = None
                msgStore = None
                msgStoresTable = None
                session.Logoff(0, 0, 0)
                session = None
                mapi.MAPIUninitialize()
                print ("DEBUG: pythoncom._GetInterfaceCount(): %i ( > 0 == 
bad)") % (pythoncom._GetInterfaceCount())

if __name__ == '__main__':
        main()
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to