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()
In the
Forwardevent handler you can find the following code:Which calls the
Forwardagain and again when the corresponding event is fired. So, theForwardmethod causes theForwardevent fired.The Forward event is fired when the user selects the Forward action for an item, or when the Forward method is called for the item. So, there is no need to call it anew in the event handler. This is a reaction to the
Forwardmethod. The new item being forwarded is passed as a parameter to the event handler.