I found the code bellow here on stackoverflow with following link: Python Outlook win32 event trigger when email is opened
I'm trying use this code and add the "def OnForward(self, disp, item):" to add a signature on the e-mail that i forward. But the forward does not work well. I will try explain, every time i click on foward it popsup the e-mail on new window, but appear on main outlook window too, and then the "def OnRead(self):" stop working too! i dont know if express myself correctly, but any help will be appreciated
import win32com.client
import pythoncom
#Handler for Application Object
class Application_Handler(object):
def OnItemLoad(self, item):
print('Application::OnItemLoad')
#Only want to work with MailItems
if( item.Class == win32com.client.constants.olMail ):
#Get a Dispatch interface to the item
cli = win32com.client.Dispatch(item)
#Set up a handler
handler = win32com.client.WithEvents(cli,MailItem_Handler)
#Store the MailItem's Dispatch interface for use later
handler.setDisp(cli)
#Handler for MailItem object
class MailItem_Handler(object):
def setDisp(self,disp):
self._disp = disp
def OnOpen(self,item):
print('MailItem::OnOpen')
def OnRead(self):
print('MailItem::OnRead')
subj = self._disp.Subject
print('Subject:',subj)
body = self._disp.Body
print('Body:',body)
def OnClose(self, item):
return
print('---------------MailItem::OnClose-------------------')
print('-----------------------------------------------------')
def OnForward(self, disp, item):
print('---------------MailItem::OnForward-------------------')
newMail = self._disp.Forward()
newMail.HTMLBody = self._disp.HTMLBody + 'teste'
newMail.Display()
print('-----------------------------------------------------')
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Application_Handler)
#Message loop
pythoncom.PumpMessages()