GetObject 函數

套用到
Microsoft 365 Access Access 2024 Access 2021 Access 2019 Access 2016

注意

如果 Microsoft Jet 運算式服務以沙箱模式執行,則會停用本主題中所述的函數、方法、物件或屬性,這會防止評估可能不安全的運算式。 如需沙箱模式的詳細資訊,請在說明中搜尋「沙箱模式」。

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

語法

GetObject ([pathname ] [, class ] )

GetObject 函數語法具有下列引數:

引數 描述
pathName 可省略。 字) (變體。 包含要檢索之物件的檔案完整路徑和名稱。 如果 省略 pathname ,則 class 為必要。
類別 可省略。 字) (變體。 代表物件類別的字串。

class引數使用語法 appname.objecttype,並具有下列部分:

部分 描述
應用程式名稱 必要。 字) (變體。 提供物件的應用程式名稱。
objectType 必要。 字) (變體。 要建立的物件類型或類別。

註解

注意

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

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


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

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

如果路徑名稱是長度為零的字串 (“”) ,GetObject 會傳回指定類型的新物件實例。 如果省略 pathname 引數, GetObject 會傳回指定類型之目前使用中的物件。 如果指定類型的物件不存在,則會發生錯誤。

某些應用程式可讓您啟用檔案的一部分。 新增驚嘆號 (!) 到檔案名稱的結尾,後面接著字串來識別您要啟用的檔案部分。 如需如何建立此字串的詳細資訊,請參閱建立物件的應用程式文件。

例如,在圖面應用程式中,您可能有多個圖層到儲存在檔案中的圖面。 您可以使用下列程式碼來啟用圖面 SCHEMA.CAD中的圖層,稱為:


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

如果未指定物件的 類別,則 Automation 會根據您提供的檔案名稱決定要啟動的應用程式和要啟動的物件。 不過,有些檔案可能支援多於一類物件。 例如,繪圖可能支援三種不同類型的物件:「 應用程式」 物件、「 繪圖」 物件和「 工具列 」物件,所有物件都是相同檔案的一部分。 若要指定您想要在檔案中啟用的物件,請使用選擇性的 class 引數。 例如:


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 函數取得特定 Excel 工作表的參照 (MyXL) 。 它會使用工作表的 Application 屬性使 Excel 可見、關閉它等等。 DetectExcel 程序會使用兩個 API 呼叫來尋找 Excel,如果它正在執行,則會將其輸入到「執行中的物件資料表」中。 如果 Microsoft Excel 尚未執行,則對 GetObject 的第一次呼叫會造成錯誤。 在範例中,這個錯誤會將 ExcelWasNotRunning 旗標設定為 True。 對 GetObject 的第二次呼叫會指定要開啟的檔案。 如果 Excel 尚未執行,則第二個呼叫將啟動它,並傳回指定檔案代表之工作表的參照 mytest.xls。 檔案必須存在於指定的位置;否則會產生 Visual Basic 錯誤自動化錯誤。 接下來,範例程式碼會讓 Excel 和包含指定工作表的視窗同時顯示。 最後,如果沒有執行舊版 Excel,程式碼會使用 Application 物件的 Quit 方法來關閉 Excel。 如果應用程式已在執行中,則不會嘗試將其關閉。 將參照本身設定為 [Nothing] 來釋放。


' 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