首先,我只想聲明我需要使用LateBinding,因爲我的程序在具有不同版本的Excel的不同機器上運行。在每個子程序中調用Latebinding子程序
我已經在一個模塊中聲明瞭一個Public Sub,該模塊可以被調用來啓動爲excel應用程序創建對象。
'Declare Public Excel Variable to Call from one place and easy manipulation
Public xlApp As Object
Public wb As Object
Public wb2 As Object
Public ws As Object
Public ws2 As Object
Public Sub InitializeExcel()
'Create the Excel Objects
Set xlApp = CreateObject("Excel.Application")
End Sub
然後在使用Excel應用程序的每個子程序,我創造,我叫InitializeExcel,並在每個月底做了以下內容:
On Error Resume Next
xlApp.Quit
Set xlApp = Nothing
Set wb = Nothing
Set wb2 = Nothing
Set ws = Nothing
Set ws2 = Nothing
我現在的問題是這樣的:
是否需要撥打InitializeExcel需要的所有子程序 excel ap或者我可以在 應用程序開始時調用它一次?
爲什麼在每個例程結束時退出excel?爲什麼不保持打開狀態直到你的應用程序退出? – MarkJ
@MarkJ - 我真的不知道。當我第一次使用Excel應用程序開始編碼時,我的朋友教會我如何做到這一點,在他的例行程序的每一端都有這樣的代碼。我從來沒有質疑過。但看到你評論它,它會影響,如果我們不關閉它,直到應用程序退出? –