2017-05-19 5 views
2

現在、私はPythonとwin32comを使用してExcelの終了イベントを取り消そうとしています。私はすでに数ヶ月前にIronPythonでこの問題を処理してきました。しかし私の企業部門のさらなる目的のためには、これはPythonでも可能でなければなりません。続いて2つのスニペットが表示されます。最初のコードにはIronPythonコードが含まれていますpythonとwin32comを使用してExcelの終了イベントをキャンセルする

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()) 

2番目のバージョンにはPythonとwin32comのバージョンが含まれています。あなたは、私が「OnBeforeClose」イベントに接続することができましたでしょうが、私はきたようにクローズイベントをキャンセルすることができないので、この1は私のIronPythonのスニペットとそのリンク

https://win32com.goermezer.de/microsoft/office/events-in-microsoft-word-and-excel.html

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()) 

のサンプルに基づいていますIronPythonのバージョンでそれを行いました。最後のコードスニペットのコメントで述べたように、PythonバージョンではAttributeError例外が発生します。さらに、イベントハンドラの必要な "キャンセル"変数のタイプには、2つの異なるタイプがあることもわかります。 IronPythonのバージョンでは、 "StrongBox [bool]"です。一方、Pythonバージョンの型は、一般的な "class 'bool'型(例外を説明しています)です。それだけで入力しようとしました

cancel = True #prevent excel from closing 

しかし、この方法では、とにかく終了します。 私もいくつかの調査をしましたが、この問題の解決策を見つけることができませんでした。私の前提では、何らかのラッパーが必要ですか?

+0

pythonnetを試しましたか? – denfromufa

+0

まずはお返事ありがとうございます。 「Microsoft.Office.Interop.Excel」への参照を追加し、IronPythonコードと同じコードを使用することを意味しますか?それは私がやろうとした最初のことでしたが、次の例外がスローされます。System.IO.FileNotFoundException:アセンブリ 'Microsoft.Office.Interop.Excel'を見つけることができません。 at Python.Runtime.CLRModule.AddReference(String name) –

+1

この 'Microsoft.Office.Interop.Excel'アセンブリの場所を見つける必要があります。それ以外の場合は、nugetからダウンロードできます。次に、その場所をsys.pathに追加して参照を追加します。 pythonnetの場合、動的なCOMオブジェクトにアクセスするための特別な手順があります:https://github.com/pythonnet/pythonnet/issues/260 – denfromufa

答えて

1

IronPythonのバージョンと同じ動作を実現するには、希望するキャンセル値を返します。

return True 

Closeイベントを中止したい場合。

よろしくお願いします。

+0

私のために働いてくれてありがとう –

関連する問題