ActiveXObject 對象
此對象提供自動化對象的接口。
function ActiveXObject(ProgID : String [, location : String])
通常,自動化服務器會提供至少一種對象。例如,字處理應用程序可能會提供應用程序對象、文檔對象和工具欄對象。
以下代碼通過調用 ActiveXObject 對象構造函數來啓動應用程序(在這種情況下爲 Microsoft Excel 工作表)。ActiveXObject 允許您在代碼中引用應用程序。使用下面的示例,您可以使用對象變量 ExcelSheet 和其他 Excel 對象(包括應用程序對象和 ActiveSheet.Cells 集合)來訪問新對象的屬性和方法。
// Declare the variables var Excel, Book; // Create the Excel application object. Excel = new ActiveXObject("Excel.Application"); // Make Excel visible. Excel.Visible = true; // Create a new work book. Book = Excel.Workbooks.Add() // Place some text in the first cell of the sheet. Book.ActiveSheet.Cells(1,1).Value = "This is column A, row 1"; // Save the sheet. Book.SaveAs("C:\\TEST.XLS"); // Close Excel with the Quit method on the Application object. Excel.Application.Quit();
若要在遠程服務器上創建對象,只能在關閉 Internet 安全機制時完成。您可以通過將計算機的名稱傳遞到 ActiveXObject 的 servername 參數在遠程網絡計算機上創建對象。該名稱與共享名的計算機名部分相同。對於名爲“\\MyServer\public”的網絡共享,servername 爲“MyServer”。此外,您可以使用 DNS 格式或 IP 地址來指定 servername。
以下代碼返回在名爲“MyServer”的遠程網絡計算機上運行的 Excel 實例的版本號:
function GetAppVersion() { var Excel = new ActiveXObject("Excel.Application", "MyServer"); return(Excel.Version); }
如果指定的遠程服務器不存在或者找不到,則會出錯。