注意
如果 Microsoft Jet 表达式服务在沙盒模式下运行,则本主题中描述的函数、方法、对象或属性将被禁用,这会阻止对潜在不安全表达式进行评估。 有关沙盒模式的详细信息,请在帮助中搜索“沙盒模式”。
返回对 ActiveX 组件提供的对象的引用。
语法
GetObject ([pathname ] [, class ] )
GetObject 函数语法具有以下参数:
| 参数 | 说明 |
|---|---|
| pathname | 可选。 字符串) (变体。 包含要检索的对象的文件的完整路径和名称。 如果省略 pathname ,则 类 为必需。 |
| 类 | 可选。 字符串) (变体。 表示对象类的字符串。 |
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")
执行此代码时,启动与指定 路径名 关联的应用程序,并激活指定文件中的对象。
如果 pathname 是零长度字符串 (“”) ,GetObject 将返回指定类型的新对象实例。 如果省略 pathname 参数, GetObject 将返回指定类型的当前活动对象。 如果指定类型的对象不存在,将出现错误。
某些应用程序允许您激活文件的一部分。 在文件名末尾添加感叹号) (! ,后面加上一个标识要激活的文件部分的字符串。 有关如何创建此字符串的信息,请参阅创建对象的应用程序的文档。
例如,在图形应用程序中,您可能有一个文件中存储的图形的多个图层。 您可以使用以下代码激活名为 SCHEMA.CAD:
Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")
如果您未指定对象 的类,Automation 将根据您提供的文件名确定要启动的应用程序和要激活的对象。 但是,某些文件可能支持多个对象类。 例如,绘图可能支持三种不同类型的对象: Application 对象、 绘图 对象和 工具栏 对象,所有这些对象都是同一文件的一部分。 若要指定要激活文件中的对象,请使用可选的 class 参数。 例如:
Dim MyObject As Object
Set MyObject = GetObject("C:\DRAWINGS\SAMPLE.DRW", _
"FIGMENT.DRAWING")
在示例中, FIGMENT 是绘图应用程序的名称, DRAWING 是其支持的对象类型之一。
激活对象后,可以使用定义的对象变量在代码中引用它。 在前面的示例中,您可以使用 object 变量 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 Sub 过程使用两个 API 调用查找 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