|
| KiXtart 2001 Manual |
COM Automation in KiXtart 2001
COM Automation is a way for applications (such as Word and Excel) to expose functionality to other applications, including scripting languages such as KiXtart. This provides an easy way to access properties and call methods of other applications from within a script.
Note: the new COM automation support in KiXtart 2001 replaces the OLE functions in previous versions of KiXtart.
Creating a COM object in KiXtart is simple:
$Object = CreateObject("WScript.Shell")
$ExcelSheet = CreateObject("Excel.Sheet")
To release all system and memory resources associated with an object, simply assign any other value to the variable:
$Object = 0
The following three sample scripts demonstrate just a few of the ways in which COM automation canbe used in KiXtart scripts. Please consult Microsoft Developer Network (MSDN) for more information on the many possibilities of COM automation.
Sample 1: script using COM automation and Active Directory Services Interface (ADSI) to retrieve various global properties of an LDAP server:
$root = GetObject("LDAP://RootDSE")
$root.GetInfo
? "ADSPath: " + $root.ADSPath
? "GUID : " + $root.GUID
? "Name : " + $root.Name
? "Parent : " + $root.Parent
? "DNC : " + $root.defaultNamingContext
Sample 2: script using COM automation and Windows Management Instrumentation (WMI) to enumerate the logical disks of the local system:
$Drives = GetObject("winmgmts:").ExecQuery("select Name,DriveType from Win32_LogicalDisk")
if @error <> 0
? @error + " / " @serror
else
for each $Drive in $Drives
? $Drive.Name
next
endif
Sample 3: script demonstrating how to start Excel and add data to a worksheet:
$oXL = CreateObject("EXCEL.application")
if @error = 0
$oXL.Visible = 1 ; make Excel visible to the user
$rc=$oXL.Workbooks.Add ; add a new workbook
$array = "Order #", "Amount", "Tax"
$oXL.Range("A1:C1").Value = $array ;add some columns
For $i = 0 To 19
$oXL.Cells(($i+2),1).Value = "ORD" + ($i + 1000)
$oXL.Cells(($i+2),2).Value = rnd() / 100
Next
;Fill the last column with a formula to compute the sales tax.
$oXL.Range("C2").Resize(20, 1).Formula = "=B2*0.07"
;Format the worksheet
$oXL.Range("A1:C1").Font.Bold = 1
$rc=$oXL.Range("A1:C1").EntireColumn.AutoFit
$oXL.UserControl = 1
else
? @error + " / " @serror
endif