這篇文章給大家介紹如何在VBS中使用CreateObject 函數,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
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
錯誤發生在指定的遠程服務器不存在或無法找到。
關于如何在VBS中使用CreateObject 函數就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。