Executing script on receipt of new email in outlook -
i need create sort of trigger in outlook python script executed new emails received in inbox. did refer link: how trigger macro run after new mail received in outlook? , have written following script:
private withevents items outlook.items private sub application_startup() dim olapp outlook.application dim objns outlook.namespace set olapp = outlook.application set objns = olapp.getnamespace("mapi") default local inbox set items = objns.getdefaultfolder(olfolderinbox).items end sub private sub test_macro(byval item object) on error goto errorhandler dim msg outlook.mailitem if typename(item) = "mailitem" set msg = item ret_val = shell("python <path-of-python-script>") debug.print "value: ", ret_val if ret_val <> 0 msgbox "couldn't run python script", vbokonly end if end if programexit: exit sub errorhandler: msgbox err.number & " - " & err.description resume programexit end sub
although not giving error due reasons python script not executing. i've configured macro settings in outlook accordingly , created new rule according documentation available. still not able achieve intended result.
any appreciated.
you need rename test_macro
sub items_itemadd
. there reason why name used in answer copied code from. items_itemadd
means sub event handler itemadd
event of object called items
.
couple of notes:
- make sure put code
thisoutlooksession
module , restart outlook initialize it. code won't work in standard module. - there no need set rules run macro. run whenever new item received (or added inbox).
- i suggest not reuse class names class instances (objects), eg. in
private withevents items outlook.items
. confusing. - to test this, copy e-mail folder (ctrl+c) , paste inbox (ctrl+v). macro should run.
try following code pasted it. (i kept confusing variable names.)
thisoutlooksession:
private withevents items outlook.items private sub application_startup() dim olapp outlook.application dim objns outlook.namespace set olapp = outlook.application set objns = olapp.getnamespace("mapi") ' default local inbox set items = objns.getdefaultfolder(olfolderinbox).items msgbox "items_itemadd listener initialized." end sub private sub items_itemadd(byval item object) on error goto errorhandler dim msg outlook.mailitem if typename(item) = "mailitem" set msg = item msgbox "python script run now." ret_val = shell("python <path-of-python-script>") debug.print "value: ", ret_val if ret_val <> 0 msgbox "couldn't run python script", vbokonly end if end if programexit: exit sub errorhandler: msgbox err.number & " - " & err.description resume programexit end sub