當您使用
CreateObject 命令存取 Microsoft Excel 做為 OLE Automation 物件時,增益集、位於 XLStart
目錄中的檔案以及預設的新活頁簿都沒有載入。
當您嘗試載入增益集和檔案時,Excel 傳送訊息給呼叫的應用程式 (例如 Visual
Basic),表示尚未準備好回應要求並且稍候再試。呼叫的程式可能無法處理這個要求,並且會繼續執行其餘的命令。
Microsoft 僅提供示範性的程式設計範例,不做任何明示或默示的保證。其中包括 (但不限於)
其適售性與適合某特定用途之默示擔保。本文假設您已相當熟悉示範所使用的程式設計語言,以及用於建立和偵錯程序的工具。Microsoft
技術支援工程師可以協助說明特定程序的功能,但不會修改這些範例以提供附加功能或建構程序來滿足您的特定需求。
如果要在您將
Excel 執行做為 OLE Automation 物件時載入增益集檔案,請手動載入增益集。這個方法的範例如下所示。
Microsoft Office Excel 2007
Sub LoadAddin()
' Dimension variable xl as object type.
Dim xl As Object
' Activate Microsoft Excel and assign to variable xl.
Set XL = CreateObject("Excel.Application")
' Open the add-in file you want, in this example, XLQUERY.XLAM.
XL.Workbooks.Open (XL.librarypath & "\MSQUERY\XLQUERY.XLAM")
' If you need to register the functions and commands
' contained in a resource (XLL), use the RegisterXLL method.
' In the example below, all functions of Analys32.xll are
' registered.
' XL.RegisterXLL "Analys32.xll"
' Run any auto macros contained in the add-in file
' Auto macros don't run when you open a file
' using the Open method.
XL.Workbooks("xlquery.xlam").RunAutoMacros 1
Set XL = Nothing
End Sub
Microsoft Office Excel 2003 和舊版的 Excel
Sub LoadAddin()
' Dimension variable xl as object type.
Dim xl As Object
' Activate Microsoft Excel and assign to variable xl.
Set XL = CreateObject("Excel.Application")
' Open the add-in file you want, in this example, XLQUERY.XLA.
XL.Workbooks.Open (XL.librarypath & "\MSQUERY\XLQUERY.XLA")
' If you need to register the functions and commands
' contained in a resource (XLL), use the RegisterXLL method.
' In the example below, all functions of Analys32.xll are
' registered.
' XL.RegisterXLL "Analys32.xll"
' Run any auto macros contained in the add-in file
' Auto macros don't run when you open a file
' using the Open method.
XL.Workbooks("xlquery.xla").RunAutoMacros 1
Set XL = Nothing
End Sub
由於 Excel 不會在您呼叫它做為 OLE Automation 物件時載入增益集或 XLStart
目錄中的檔案,因此您對於載入程序具有完全的控制權。如果要載入增益集,您可以手動載入。
這個行為也表示,在藉由
CreateObject 命令呼叫 Excel 時,沒有巨集錯誤、鎖定檔案警示或唯讀檔案警示能夠限制 Excel 載入。這個行為的另一個好處是,載入
Excel 的時間會比在增益集或檔案已載入的情況下進行載入所花費的時間更少。
如需有關如何取得 Visual Basic for Applications
說明的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
226118?
(http://support.microsoft.com/kb/226118/
)
List of resources that are available to help you learn Visual Basic for Applications programming