CreateObject 函数CreateObject function

创建并返回对 ActiveX 对象的引用。Creates and returns a reference to an ActiveX object.

语法Syntax

CreateObject(class, [ servername ])CreateObject(class, [ servername ])


CreateObject 函数语法包含以下部分:The CreateObject function syntax has these parts:

PartPart 说明Description
classclass 必需;Variant (String)。Required; Variant (String). 要创建对象的应用程序名称和类。The application name and class of the object to create.
servernameservername 可选;Variant (String)。Optional; Variant (String). 将在其中创建对象的网络服务器的名称。The name of the network server where the object will be created. 如果 servername 为空字符串 (""),则使用本地计算机。If servername is an empty string (""), the local machine is used.

class 参数 使用 appname_.objecttype_ 语法并包含以下部分:The class argument uses the syntax appname.objecttype and has these parts:

PartPart 说明Description
appnameappname 必需;Variant (String)。Required; Variant (String). 提供对象的应用程序的名称。The name of the application providing the object.
objecttypeobjecttype 必需;Variant (String)。Required; Variant (String). 要创建对象的类型或The type or class of object to create.

注解Remarks

每个支持 Automation 的应用程序都提供至少一种类型的对象。Every application that supports Automation provides at least one type of object. 例如,字处理应用程序可能提供 Application 对象、Document 对象和 Toolbar 对象。For example, a word processing application may provide an Application object, a Document object, and a Toolbar object.

要创建 ActiveX 对象,请将 CreateObject 返回的对象指定给对象变量To create an ActiveX object, assign the object returned by CreateObject to an object variable.

' Declare an object variable to hold the object 
' reference. Dim as Object causes late binding. 
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")

此代码启动创建对象的应用程序,在本例中是 Microsoft Excel 电子表格。This code starts the application creating the object, in this case, a Microsoft Excel spreadsheet. 创建对象后,就可以使用定义的对象变量在代码中对其进行引用。After an object is created, you reference it in code by using the object variable you defined. 在下面的示例中,访问使用对象变量的新对象的属性方法ExcelSheet 以及其他 Microsoft Excel 对象(包括 Application 对象与 Cells 集合)。In the following example, you access properties and methods of the new object by using the object variable, ExcelSheet, and other Microsoft Excel objects, including the Application object and the Cells collection.

' Make Excel visible through the Application object.
ExcelSheet.Application.Visible = True
' Place some text in the first cell of the sheet.
ExcelSheet.Application.Cells(1, 1).Value = "This is column A, row 1"
' Save the sheet to C:\test.xls directory.
ExcelSheet.SaveAs "C:\TEST.XLS"
' Close Excel with the Quit method on the Application object.
ExcelSheet.Application.Quit
' Release the object variable.
Set ExcelSheet = Nothing


使用 As Object 子句声明对象变量将创建可包含对任意类型对象的引用的变量。Declaring an object variable with the As Object clause creates a variable that can contain a reference to any type of object. 但是,通过该变量访问该对象属于晚期绑定(即,在程序运行时绑定)。However, access to the object through that variable is late bound; that is, the binding occurs when your program is run. 要创建导致早期绑定(即,在编译程序时绑定)的对象变量,请使用特定的类 ID 声明对象变量。To create an object variable that results in early binding, that is, binding when the program is compiled, declare the object variable with a specific class ID. 例如,可以声明并创建以下 Microsoft Excel 引用:For example, you can declare and create the following Microsoft Excel references:

Dim xlApp As Excel.Application 
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.WorkSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)


通过早期绑定变量进行引用可实现更佳的性能,但只能包含对声明中所指定的引用。The reference through an early-bound variable can give better performance, but can only contain a reference to the class specified in the declaration.

可以将 CreateObject 函数返回的对象传递到预期对象为参数的函数。You can pass an object returned by the CreateObject function to a function expecting an object as an argument. 例如,使用以下代码创建并传递对 Excel.Application 对象的引用:For example, the following code creates and passes a reference to a Excel.Application object:

Call MySub (CreateObject("Excel.Application"))

可以通过将计算机的名称传递给 CreateObjectservername 参数在远程网络计算机上创建对象。You can create an object on a remote networked computer by passing the name of the computer to the servername argument of CreateObject. 该名称与共享名称的“计算机名称”部分相同;如果共享名称为“\MyServer\Public”,则 servername 为“MyServer”。That name is the same as the Machine Name portion of a share name; for a share named "\MyServer\Public," servername is "MyServer."

以下代码返回在名为 MyServer 的远程计算机上运行的 Excel 实例的版本号:The following code returns the version number of an instance of Excel running on a remote computer named MyServer:

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application", "MyServer")
Debug.Print xlApp.Version

如果远程服务器不存在或不可用,将出现运行时错误。If the remote server doesn't exist or is unavailable, a run-time error occurs.

备注

如果没有对象的当前实例,请使用 CreateObjectUse CreateObject when there is no current instance of the object. 如果对象实例已在运行,则启动新的实例并创建指定类型的对象。If an instance of the object is already running, a new instance is started, and an object of the specified type is created. 要使用当前实例,或启动应用程序并加载文件,请使用 GetObject 函数。To use the current instance, or to start the application and have it load a file, use the GetObject function.

如果对象本身注册为单实例对象,无论执行多少次 CreateObject 都只创建一个对象实例。If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times CreateObject is executed.

示例Example

本示例使用 CreateObject 函数来设置对 Microsoft Excel 的引用 (xlApp)。This example uses the CreateObject function to set a reference (xlApp) to Microsoft Excel. 其通过引用访问 Microsoft Excel 的 Visible 属性,再使用 Microsoft Excel Quit 方法进行关闭。It uses the reference to access the Visible property of Microsoft Excel, and then uses the Microsoft Excel Quit method to close it. 最后,释放引用本身。Finally, the reference itself is released.

Dim xlApp As Object    ' Declare variable to hold the reference.
    
Set xlApp = CreateObject("excel.application")
    ' You may have to set Visible property to True
    ' if you want to see the application.
xlApp.Visible = True
    ' Use xlApp to access Microsoft Excel's 
    ' other objects.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.