GetObject 函式

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

語法

GetObject ([ pathname ], [ class ])

GetObject函式語法具有下列具名引數

部分 描述
pathname 選用; 變體 (字串)。 包含要擷取之物件之檔案的完整路徑和名稱。 如果省略 pathname ,則需要 類別
類別 選用; 變體 (字串)。 字串,表示 物件的 類別

類別引數會使用語法appnameobjecttype和 具有下列部分:

部分 描述
appname 需要此項目; 變體 (字串)。 提供物件的應用程式名稱。
objecttype 需要此項目; 變體 (字串)。 輸入要建立的物件類型或類別。

註解

使用 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")

如果您未指定物件的 類別,自動化會根據您提供的檔案名,決定要啟動的應用程式和要啟動的物件。 不過,某些檔案可能支援多個類別的 物件。 例如,繪圖可能支援三種不同類型的物件: Application 物件、 Drawing 物件和 Toolbar 物件,這些全都是相同檔案的一部分。 若要指定要啟動檔案中的物件,請使用選擇性 類別 引數。 例如:

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 Excel 工作表 () MyXL 的參考。 它會使用工作 表的 Application 屬性,讓 Microsoft Excel 可見、關閉它等等。

使用兩個 API 呼叫, DetectExcelSub程式會尋找 Microsoft Excel,如果正在執行,則會在執行中的物件資料表中輸入它。

如果 Microsoft Excel 尚未執行,第一次呼叫 GetObject 會造成錯誤。 在此範例中,錯誤會使 ExcelWasNotRunning 旗標設定為 True

第二次呼叫 GetObject 會 指定要開啟的檔案。 如果 Microsoft Excel 尚未執行,則第二個呼叫會啟動它,並傳回指定檔案所代表之工作表的參考,mytest.xls。 檔案必須存在於指定的位置;否則會產生 Visual Basic 錯誤 Automation error

接下來,範例程式碼會顯示 Microsoft Excel 和包含指定工作表的視窗。 最後,如果沒有執行的舊版 Microsoft Excel,程式碼會使用 Application 物件的 Quit 方法來關閉 Microsoft 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

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應