CreateObject 函數(shù)
創(chuàng)建并返回對(duì) Automation 對(duì)象的引用。
CreateObject(servername.typename [, location])
參數(shù)
servername
必選項(xiàng)。提供對(duì)象的應(yīng)用程序名稱。
typename
必選項(xiàng)。要?jiǎng)?chuàng)建的對(duì)象類型或類。
location
可選項(xiàng)。對(duì)象所在的網(wǎng)絡(luò)服務(wù)器將被創(chuàng)建。
說(shuō)明
Automation 服務(wù)器至少提供一種對(duì)象類型。例如,字處理應(yīng)用程序可以提供應(yīng)用程序?qū)ο蟆⑽臋n對(duì)象和工具條對(duì)象。
要?jiǎng)?chuàng)建 Automation 對(duì)象,將 CreateObject 函數(shù)返回的對(duì)象賦值給某對(duì)象變量:
Dim ExcelSheet
Set ExcelSheet = CreateObject("Excel.Sheet")
上述代碼啟動(dòng)創(chuàng)建對(duì)象(在此實(shí)例中,是 Microsoft Excel 電子表格)的應(yīng)用程序。對(duì)象創(chuàng)建后,就可以在代碼中使用定義的對(duì)象變量引用此對(duì)象。在下面的示例中,可使用對(duì)象變量、ExcelSheet 和其他 Excel 對(duì)象,包括 Application 對(duì)象和 Cells 集合訪問(wèn)新對(duì)象的屬性和方法。例如:
' 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
在遠(yuǎn)程服務(wù)器上創(chuàng)建一個(gè)對(duì)象,當(dāng) Internet 安全關(guān)閉時(shí)只能完成。通過(guò)傳遞計(jì)算機(jī)名到 CreateObject 服務(wù)器名的參數(shù),能在遠(yuǎn)程網(wǎng)絡(luò)上創(chuàng)建對(duì)象。該名稱如同共享部份的機(jī)器名。例如網(wǎng)絡(luò)共享名命名為: "\\myserver\public", servername 是 "myserver"。另外,只能指定 servername 使用 DNS 格式或 IP 地址。
以下代碼返回運(yùn)行在命名為"myserver"的遠(yuǎn)程網(wǎng)絡(luò)計(jì)算機(jī)上 Excel 實(shí)例的版本號(hào) :
Function GetVersion
Dim XLApp
Set XLApp = CreateObject("Excel.Application", "MyServer")
GetVersion = XLApp.Version
End Function
錯(cuò)誤發(fā)生在指定的遠(yuǎn)程服務(wù)器不存在或無(wú)法找到。