注意
若 Microsoft Jet Expression Service 以沙盒模式執行,則本主題所述的函式、方法、物件或屬性將被禁用,避免評估潛在不安全的表達式。 想了解更多沙盒模式的資訊,請在說明中搜尋「sandbox mode」。
回傳由 ActiveX 元件提供的物件參考。
語法
GetObject ([路徑名稱 ] [, 類別 ] )
GetObject 函式語法包含以下參數:
| 引數 | 描述 |
|---|---|
| 路徑名稱 | 可省略。 弦 ) (變體。 包含要取回物件的完整路徑與檔案名稱。 若路徑 名稱 省略,則需使用 類別 。 |
| 類別 | 可省略。 弦 ) (變體。 一個代表物件類別的字串。 |
類別參數使用語法 appname.objecttype,包含以下部分:
| 部分 | 描述 |
|---|---|
| 應用程式名稱 | 必要。 弦 ) (變體。 提供該物件的應用程式名稱。 |
| 物件類型 | 必要。 弦 ) (變體。 要建立的物件類型或類別。 |
註解
注意
下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。 如需使用 VBA 的詳細資訊,請在 [搜尋] 旁的下拉式清單中選取 [開發人員參考],並在 [搜尋] 方塊中輸入一個或多個字詞。
使用 GetObject 函式從檔案存取 ActiveX 物件,並將該物件指派到物件變數。 使用 Set 陳述式將 GetObject 回傳的物件指派到物件變數。 例如:
Dim CADObject As Object
Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")
當執行此程式碼時,與指定 路徑名稱 相關的應用程式會被啟動,並啟動指定檔案中的物件。
如果路徑名稱是零長度的字串 (“”) ,GetObject 會回傳指定型別的新物件實例。 若遺漏 路徑名稱 參數, GetObject 會回傳指定型別的當前啟用物件。 若不存在指定類型的物件,則會發生錯誤。
有些應用程式允許你啟用部分檔案。 在檔案名稱末尾加個驚嘆號 (!,) 後面加上一個字串,標示你想啟用的檔案部分。 關於如何建立此字串,請參閱建立該物件的應用程式文件。
例如,在繪圖應用程式中,你可能會將多個圖層儲存在檔案中。 你可以用以下程式碼在繪圖中啟動一個稱為 SCHEMA.CAD:
Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")
如果你沒指定物件的 類別,自動化會根據你提供的檔名決定啟動的應用程式和啟用的物件。 然而,有些檔案可能支援多個物件類別。 例如,繪圖可能支援三種不同類型的物件: 應用程式 物件、 繪圖 物件和 工具列 物件,這些物件都屬於同一檔案。 要指定你想啟用的檔案物件,請使用可選的 類別 參數。 例如:
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 在使用零長度字串 (“”) 語法時,總是回傳相同的實例,若遺漏 路徑名稱 參數則會出錯。 你無法用 GetObject 取得用 Visual Basic 建立的類別的參考。
範例
這個範例使用 GetObject 函式來取得特定 Excel 工作表的參考, (MyXL) 。 它會利用工作表的 應用程式 屬性來讓 Excel 顯示、關閉它等等。 透過兩個 API 呼叫,DetectExcel 子 程序會尋找 Excel,若正在執行,則將其輸入 Running Object Table。 如果 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