使用python和win32com取消Excel的closures事件

目前我尝试使用Python和win32com取消Excel的closures事件。 我已经设法在一个月前用IronPython来处理这个问题。 但为了我公司部门的进一步目的,这也应该能够与Python。 接下来你会看到两个片段。 第一个将包含IronPython代码

import clr clr.AddReference("Microsoft.Office.Interop.Excel") clr.AddReference("System.Windows.Forms") from Microsoft.Office.Interop import Excel from System.Windows.Forms import Form, Application, MessageBox, MessageBoxButtons, MessageBoxIcon, DialogResult class CloseEventTry(Form): def __init__(self): excel = Excel.ApplicationClass() excel.Visible = True excel.DisplayAlerts = False self.workbooks = excel.Workbooks.Add() self.Text = "Dummy GUI Window" #link "BeforeCloseEvent" to the "beforeClose" method self.workbooks.BeforeClose +=Excel.WorkbookEvents_BeforeCloseEventHandler(self.beforeClose) def beforeClose(self, cancel): print type(cancel) #Type: 'StrongBox[bool] choice = MessageBox.Show("Close Excel", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Information) if choice == DialogResult.Yes: cancel.Value = False #do't cancel the close action self.Close() elif choice == DialogResult.No: cancel.Value = True #prevent excel from closing Application.Run(CloseEventTry()) 

第二个将包含Python和win32com的版本。 这是基于我的IronPython片段和该链接的示例

Events in Microsoft Word and Excel

 import clr clr.AddReference("System.Windows.Forms") from System.Windows.Forms import Form, Application, MessageBox, MessageBoxButtons, MessageBoxIcon, DialogResult import win32com.client as win32 #workbook event handler class. Needed according to this example https://win32com.goermezer.de/microsoft/office/events-in-microsoft-word-and-excel.html class WorkBookEvents(object): def OnBeforeClose(self, cancel): print(type(cancel)) #Type: class 'bool' choice = MessageBox.Show("Close Excel", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Information) if choice == DialogResult.Yes: #do't cancel the close action => raises AttributeError: 'bool' object has no attribute 'Value' Exception cancel.Value = False self.Close() elif choice == DialogResult.No: #prevent excel from closing => raises AttributeError: 'bool' object has no attribute 'Value' Exception cancel.Value = True class CloseEventTry(Form): def __init__(self): excel = win32.DispatchEx('Excel.Application') excel.Visible = True # makes the Excel application visible to the user excel.DisplayAlerts = False self.Text = "Dummy GUI Window" self.workbooks = excel.Workbooks.Add() #define event handler according to this example https://win32com.goermezer.de/microsoft/office/events-in-microsoft-word-and-excel.html self.workbooks = win32.DispatchWithEvents(self.workbooks, WorkBookEvents) Application.Run(CloseEventTry()) 

正如你所看到的,我可以连接到“OnBeforeClose”事件,但不能像IronPython版本那样取消closures事件。 正如上一个代码片段的注释中提到的,Python版本引发了一个AttributeErrorexception。 此外,您还可以看到,事件处理程序所需的“取消”variables的types有两种不同的types。 在IronPython版本中,它是一个“StrongBox [bool]”。 另一方面,Python版本的types是一个常见的“class”bool“”types(这解释了exception)。 这就是我试图打字

 cancel = True #prevent excel from closing 

但是用这种方式,excelclosures了。 我也做了一些研究,但无法find解决这个问题的方法。 我的假设是有一些包装需要?

Interesting Posts