【发布时间】:2022-01-19 19:34:09
【问题描述】:
我在 stackoverflow 上找到了下面的代码,链接如下:Python Outlook win32 event trigger when email is opened
我正在尝试使用此代码并添加“def OnForward(self, disp, item):”以在我转发的电子邮件上添加签名。但是前锋不好用。我会尝试解释一下,每次我点击转发时,它都会在新窗口中弹出电子邮件,但也会出现在主 Outlook 窗口中,然后“def OnRead(self):”也停止工作!我不知道是否正确表达自己,但任何帮助将不胜感激
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()
【问题讨论】:
-
您有正确的 OnForward 签名吗?这是参考:docs.microsoft.com/en-us/office/vba/api/… 参数是 OnForward(self,item,bCancel)。
-
我更正了上面引用的链接,因为我在 OnOpen 签名上出错了。
标签: python outlook outlook-addin win32com win32ole