使用 Microsoft 登入
登入或建立帳戶。
您好:
選取其他帳戶。
您有多個帳戶
選擇您要用來登入的帳戶。

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

傳回 ActiveX 元件所提供的物件參照。

語法

GetObject ([pathname ] [, class ] )

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

引數

描述

路徑

選擇性。 Variant (String) 。 包含要擷取之物件之檔案的完整路徑和名稱。 如果省略 pathname ,則必須 使用 class

類別

選擇性。 Variant (String) 。 代表物件 類別 的字串。


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

部分

描述

appname

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

objecttype

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


註解

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

使用 GetObject 函數從檔案存取 ActiveX 物件,並將物件指派給 物件變數。 使用 Set 語句將 GetObject 傳回的物件指派給物件變數。 例如:

Dim CADObject As Object
Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")

執行此程式代碼時,會啟動與指定 路徑名稱 相關聯的應用程式,並啟用指定檔案中的物件。

如果 pathname 是零長度字串 ( ) ,GetObject 會 傳回指定類型的新物件實例。 如果省略 pathname 自變數, GetObject 會 傳回指定類型的目前作用中物件。 如果未存在指定類型的物件,則會發生錯誤。

某些應用程式可讓您啟用檔案的一部分。 將驚嘆號 () 新增至檔名的結尾,並以識別您要啟用之檔案部分的字串加以追蹤。 如需如何建立此字串的相關信息,請參閱建立物件之應用程式的檔。

例如,在繪圖應用程式中,您可能會在儲存在檔案中的繪圖中擁有多個圖層。 您可以使用下列程式代碼來啟用繪圖中名為 SCHEMA.CAD的層次:

Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")

如果您沒有指定物件的 類別,Automation 會根據您提供的檔名決定要啟動的應用程式和要啟用的物件。 不過,某些檔案可能支援多個類別的物件。 例如,繪圖可能支援三種不同類型的物件: Application 對象、 繪圖 物件和 工具列 對象,它們都屬於同一個檔案的一部分。 若要在檔案中指定您要啟用的物件,請使用選用的 類別 自變數。 例如:

Dim MyObject As Object
Set MyObject = GetObject("C:\DRAWINGS\SAMPLE.DRW", _
"FIGMENT.DRAWING")

在範例中, FIGMENT 是繪圖應用程式的名稱,而 DRAWING 是其支援的物件類型之一。

對象啟動後,您會使用您定義的物件變數,在程式代碼中參照該物件。 在上述範例中,您使用物件變數 MyObject存取新對象的屬性和方法。 例如:

MyObject.Line 9, 90
MyObject.InsertText 9, 100, "Hello, world."
MyObject.SaveAs "C:\DRAWINGS\SAMPLE.DRW"

附註:  如果有物件的目前實例,或如果您想要使用已載入的檔案建立物件,請使用 GetObject 函數。 如果沒有目前的實例,而且您不希望對象開始載入檔案,請使用 CreateObject 函數。

如果對象已註冊為單一實例物件,則無論 執行 CreateObject 多少次,只會建立一個對象實例。 使用單一實例物件時, GetObject 會在使用零長度字元串 (“) 語法撥號時,一律傳回相同的實例,如果省略 pathname 自變數,則會造成錯誤。 您無法使用 GetObject 取得使用 Visual Basic 建立之班級的參照。

範例

此範例使用 GetObject 函數來取得特定 Microsoft Office Excel 2007 工作表 (MyXL) 的參照。 它會使用工作表的 Application 屬性來顯示 Excel、關閉它,依此類推。 使用兩個 API 呼叫,DetectExcel Sub 程式會尋找 Excel,如果是執行中,請在 [執行物件表格] 中輸入。 如果 Microsoft Excel 尚未執行,第一次 撥打 GetObject 會 造成錯誤。 在範例中,錯誤會造成 ExcelWasNotRunning 旗標設為 True。 GetObject 的第二個通話會指定要開啟的檔案。 如果 Excel 尚未執行,第二個通話會啟動,並傳回由指定檔案表示之工作表的參照,mytest.xls。 檔案必須存在於指定的位置;否則,會產生 Visual Basic 錯誤自動化錯誤。 接下來,範例程式代碼會同時顯示 Excel 和包含指定工作表的視窗。 最後,如果沒有舊版的 Excel 在執行,程式代碼會使用 Application 物件的 Quit 方法來關閉 Excel。 如果應用程式已在執行中,則不會嘗試將其關閉。 將參照設定為 [ 無],即可釋放參照本身。

' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName as String, _
ByVal lpWindowName As Long) As Long
Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long, _
ByVal wParam as Long, _
ByVal lParam As Long) As Long
Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.
' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' GetObject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.
' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel
' Set the object variable to reference the file you want to see.
Set MyXL = GetObject("c:\vb4\MYTEST.XLS")
' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
Do manipulations of your file here.
' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End IF
Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.
End Sub
Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub

需要更多協助嗎?

想要其他選項嗎?

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

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

這項資訊有幫助嗎?

您對語言品質的滿意度如何?
以下何者是您會在意的事項?
按下 [提交] 後,您的意見反應將用來改善 Microsoft 產品與服務。 您的 IT 管理員將能夠收集這些資料。 隱私權聲明。

感謝您的意見反應!

×