用户登录
用户注册

分享至

VBS教程:函数-CreateObject 函数

  • 作者: 人生就像蒲公英看似自由却身不由己
  • 来源: 51数据库
  • 2021-08-25

createobject 函数

创建并返回对 automation 对象的引用。

createobject(servername.typename [, location])

参数

servername

必选项。提供对象的应用程序名称。

typename

必选项。要创建的对象类型或类。

location

可选项。对象所在的网络服务器将被创建。

说明

automation 服务器至少提供一种对象类型。例如,字处理应用程序可以提供应用程序对象、文档对象和工具条对象。

要创建 automation 对象,将 createobject 函数返回的对象赋值给某对象变量:

dim excelsheet
set excelsheet = createobject("excel.sheet")

上述代码启动创建对象(在此实例中,是 microsoft excel 电子表格)的应用程序。对象创建后,就可以在代码中使用定义的对象变量引用此对象。在下面的示例中,可使用对象变量、excelsheet 和其他 excel 对象,包括 application 对象和 cells 集合访问新对象的属性和方法。例如:

' make excel visible through the application object.
excelsheet.application.visible = true
' place some text in the first cell of the sheet.
excelsheet.activesheet.cells(1,1).value = "this is column a, row 1"
' save the sheet.
excelsheet.saveas "c:\docs\test.xls"
' close excel with the quit method on the application object.
excelsheet.application.quit
' release the object variable.
set excelsheet = nothing

在远程服务器上创建一个对象,当 internet 安全关闭时只能完成。通过传递计算机名到 createobject 服务器名的参数,能在远程网络上创建对象。该名称如同共享部份的机器名。例如网络共享名命名为: "\\myserver\public", servername 是 "myserver"。另外,只能指定 servername 使用 dns 格式或 ip 地址。

以下代码返回运行在命名为"myserver"的远程网络计算机上 excel 实例的版本号 :

function getversion
 dim xlapp
 set xlapp = createobject("excel.application", "myserver")
 getversion = xlapp.version
end function

错误发生在指定的远程服务器不存在或无法找到。

软件
前端设计
程序设计
Java相关