Events in Microsoft Project

I found this very interesting Python script, which showes how to use Events in Microsoft Project with Python for Windows. Simply dispatch with an eventmanager class:

import win32com.client
import pythoncom

defaultNamedNotOptArg = pythoncom.Empty

class EventManager(object):
    def OnProjectBeforeSave(self, pj=defaultNamedNotOptArg, SaveAsUi=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Project saved'
        pass
    def OnProjectBeforeClose(self, pj=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Project closed'
        pass
    def OnProjectTaskNew(self, pj=defaultNamedNotOptArg, ID=defaultNamedNotOptArg):
        print 'Task created'
        pass
    def OnProjectBeforeTaskChange(self, tsk=defaultNamedNotOptArg, Field=defaultNamedNotOptArg, NewVal=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Field %s of Task %s changed to %s'%(Field, tsk, NewVal)
        pass
    def OnProjectBeforeTaskDelete(self, tsk=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Task %s deleted'%tsk
        pass

app = win32com.client.DispatchWithEvents('MSProject.Application', EventManager)
doc = app.ActiveProject
app.Visible=1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.