GetObject 函数

返回对 ActiveX 组件提供的对象的引用。

语法

GetObject ([ pathname ], [ class ])

GetObject 函数语法包含以下命名参数

Part 说明
pathname 可选;Variant (String)。 包含要检索的对象的文件的完整路径和名称。 如果省略了 pathname,则 class 是必须的。
class 可选;Variant (String)。 表示对象的的字符串。

参数使用语法 appnameobjecttype 和 具有以下部分:

Part 说明
appname 必需;Variant (String)。 提供对象的应用程序的名称。
objecttype 必需;Variant (String)。 提供要创建的对象的类的类型。

备注

使用 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 对象,所有这些对象都是同一文件的一个部分。 若要指定文件中要激活的对象,请使用可选的 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 函数获取对特定 Microsoft Excel 工作表 (MyXL) 的引用。 本示例使用工作表的 Application 属性执行使 Microsoft Excel 可见、关闭 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 支持和反馈,获取有关如何接收支持和提供反馈的指南。