2011-12-01 38 views
0

使用以下python方法,我調用了Excel宏。當我開始工作時,我很高興,但是我想知道每次執行這個操作時,我都可以看到一個與我使用Macro的.XLA同名的Windows臨時/鎖定文件。使用Python運行Excel宏(但Excel進程保留在內存中)

class XlaMakeUnmake: 

    __configFile = 'xla_svn.cfg' 
    __vbaTools = 'makeProtected.xla' 
    __entries = {} 

    def __init__(self): 
     self.__readConfig() 

    def __newDirs(self, dir): 
     try: 
      os.makedirs(dir) 
     except OSError, e: 
      if e.errno != errno.EEXIST: 
       raise 

    ### WILL ONLY WORK FOR UNPROTECTED 
    ''' 
    filePath: path of XLA to unmake 
    outputDir: folder in which the lightweight xla and its components will be pushed to 
    ''' 
    def unmake(self, filePath, outputDir): 
     xl = win32com.client.Dispatch("Excel.Application") 
     xl.Visible = 0 
     self.__newDirs(outputDir) 
     xl.Application.Run("'" + os.getcwd() + os.sep + self.__vbaTools + "'!Unmake", filePath, outputDir) 

當我打開任務管理器,我可以看到一個Excel進程仍在運行...如何殺死它,但以清潔的方式,當任務完成? xl.Application.Run是否啓動對宏的異步調用?在這種情況下,它可能會很棘手......

謝謝你們! ;)

+1

你需要釋放Excel應用程序的實例。不知道如何在Python中做到這一點;在VB中,你會設置'xl = Nothing',在Delphi中你會設置'xl:= nil;',如果有幫助的話。 –

+0

我試圖做一個「xl =無」,但沒有效果 – Jerome

回答

2

我不知道Python,但你需要使用:

xl.Quit 
+0

我試過後,「xl.Application.Run」,蟒蛇沒有抱怨,但沒有效果。 – Jerome

+0

啊,我很愚蠢,我不得不做xl.Quit(),否則python把它作爲對象的新屬性。有用 ! – Jerome