Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

附註: 如果 Microsoft Jet Expression Service 在沙箱模式中執行,會停用本主題中所述的函數、方法、物件或屬性,以免評估可能不安全的表達式。 如需沙盒模式的詳細資訊,請在 [說明] 中搜尋「沙盒模式」。

建立並傳回 ActiveX 物件的參照。

語法

CreateObject ( class [, servername] )

CreateObject 函數語法具有下列自變數:

引數

描述

類別

必要。 Variant (String) 。 要建立之物件的應用程式名稱和類別。

伺服器名稱

選擇性。 Variant (String) 。 要建立對象的網路伺服器名稱。 如果 伺服器名稱 是空字串 (“) ,則會使用本機計算機。


班級 引數 使用語法 appnameobjecttype ,並具有下列部分:

部分

描述

appname

必要。 Variant (String) 。 提供物件的應用程式名稱。

objecttype

必要。 Variant (String) 。 要建立的物件類型或 類別 。


註解

每個支援自動化的應用程式都會提供至少一種類型的物件。 例如,字處理應用程式可能會提供 Application 物件、 檔案 物件和 工具列 物件。

若要建立 ActiveX 物件,請將 CreateObject 傳回的物件指派給 物件變數:

附註: 下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。 如需使用 VBA 的詳細資訊,請在 [搜尋] 旁的下拉式清單中選取 [開發人員參考],並在 [搜尋] 方塊中輸入一個或多個字詞。

' Declare an object variable to hold the object 
' reference. Dim as Object causes late binding.
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")

在此範例中,我們將會從 Access 資料庫中自動化 Excel 電子表格物件。 這個程式代碼會啟動應用程式,以建立物件,在此情況下是 Microsoft Excel 電子表格。 物件建立後,您會使用您定義的物件變數,在程式代碼中參照該物件。 在下列範例中,您使用物件變數、 ExcelSheet和其他 Excel 物件存取新物件的屬性和方法,包括 Application 物件和 Cells 集合。

' Make Excel visible through the Application object.
ExcelSheet.Application.Visible = True
' Place some text in the first cell of the sheet.
ExcelSheet.Application.Cells(1, 1).Value = "This is column A, row 1"
' Save the sheet to C:\test.xls directory.
ExcelSheet.SaveAs "C:\TEST.XLS"
' Close Excel with the Quit method on the Application object.
ExcelSheet.Application.Quit
' Release the object variable.
Set ExcelSheet = Nothing

使用 As Object 子句宣告物件變數會建立一個可包含任何類型物件參照的變數。 不過,透過該變數存取物件的許可權較晚;也就是說,當您的程序執行時,就會產生系結。 若要建立能提早系結的物件變數,也就是在程式編譯時系結,請使用特定的類別標識符宣告物件變數。 例如,您可以宣告並建立下列 Excel 參照:

Dim xlApp As Excel.Application 
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.WorkSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)

透過預先系結變數的參照可以提供更好的效能,但只能包含 宣告中指定之 類別 的參照。

您可以將 CreateObject 函數傳回的物件傳遞至預期物件為自變數的函數。 例如,下列程式代碼會建立並傳遞 Excel.Application 對象的參照:

Call MySub (CreateObject("Excel.Application"))

您可以透過將計算機名稱傳遞至 CreateObject伺服器名稱自變數,在遠端網路電腦上建立物件。 該名稱與共享名稱的 [計算機名稱] 部分相同:對於名為 “\\MyServer\Public” 的共用, 伺服器名稱 是 “MyServer”。

附註:  如需在遠端網路計算機上顯示應用程式的其他資訊,請參閱 COM 檔案 (請參閱 Microsoft Developer Network) 。 您可能需要為應用程式新增登錄機碼。

下列程式代碼會傳回在名為 MyServer的遠端電腦上執行之 Excel 實例的版本號碼:

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application", "MyServer")
Debug.Print xlApp.Version

如果遠端伺服器不存在或無法使用,就會發生運行時間錯誤。

附註:  當物件目前沒有實例時,請使用 CreateObject 。 如果物件實例已在執行中,則會開始新的實例,並建立指定類型的物件。 若要使用目前的實例,或是啟動應用程式並載入檔案,請使用 GetObject 函數。

如果對象已註冊為單一實例物件,則無論 執行 CreateObject 多少次,只會建立一個對象實例。

範例

此範例使用 CreateObject 函數來設定參照 (

xlApp

) 至 Excel。 它會使用參照來存取 Excel 的 Visible 屬性,然後使用 Excel 結束 方法將其關閉。 最後,會發行參照本身。

Dim xlApp As Object    ' Declare variable to hold the reference.
Set xlApp = CreateObject("excel.application")
' You may have to set Visible property to True
' if you want to see the application.
xlApp.Visible = True
' Use xlApp to access Microsoft Excel's
' other objects.
xlApp.Quit ' When you finish, use the Quit method to close
Set xlApp = Nothing ' the application, then release the reference.

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×