Sys.OleObject Property

Applies to TestComplete 15.62, last modified on March 19, 2024

Description

The Sys.OleObject property returns an OLE server by its name and the name of the machine on which it is running. To specify the OLE server running on the local machine, do not specify the MachineName parameter.

Declaration

Sys.OleObject(OleObject, MachineName)

Read-Only Property Object
OleObject [in]    Required    String    
MachineName [in]    Optional    String Default value: ""   

Applies To

The property is applied to the following object:

Sys

Parameters

The property has the following parameters:

OleObject

ProgID or CLSID of the OLE server you want to access. For example, "Word.Application" or "{000209FF-0000-0000-C000-000000000046}".

MachineName

Specifies the name of the machine on which the specified OLE server is running. To get the OLE server running on the local machine, leave the value of this parameter empty.

Property Value

An object that represents the specified OLE server.

Remarks

  • You cannot use TestComplete as a client application for in-process OLE objects, the bitness of which is different than the bitness of TestComplete.

  • TestComplete does not release instances of OLE objects created via the Sys.OleObject method at design time, that is, when this method is used to explore the object in the Object Browser or to get code completion for the object’s methods and properties. For example, creating a Word.Application object launches Microsoft Word and it keeps running after you have finished working with the object in the Object Browser or the Code Editor. To release the used OLE object instance, you need to manually close the process that the object is running in or to execute the script code that would release that object, for example:

    JavaScript, JScript

    function QuitWord()
    {
      Sys.OleObject("Word.Application").Quit();
    }

    Python

    def QuitWord():
      Sys.OleObject["Word.Application"].Quit()

    VBScript

    Sub QuitWord
      Sys.OleObject("Word.Application").Quit
    End Sub

    DelphiScript

    procedure QuitWord;
    begin
      Sys.OleObject('Word.Application').Quit;
    end;

    C++Script, C#Script

    function QuitWord()
    {
      Sys["OleObject"]("Word.Application")["Quit"]();
    }

  • Note for Microsoft Office users: In order to be able to get an OLE object for an already running application, this application must be registered in the Running Object Table (ROT). However, Microsoft Office applications that are launched from the shell (for example, from the Start menu or the TestedApps project item) do not register their running objects at startup, but rather once the application loses focus. So, if you obtain an OLE object for an already running Office application, you may get errors when accessing its properties and methods (for more information on this problem, see http://support.microsoft.com/kb/238610/).

    To work around the problem, you can activate any window other than the tested Office application before attaching to its OLE server. This will allow the Office application to register itself in the ROT, so you will be able to work with it via OLE. In scripts, you can activate a window using the Activate or Click action. For example, use the following statement to activate the Windows taskbar:

    JavaScript, JScript

    Sys.Process("Explorer").Window("Shell_TrayWnd").Activate();

    Python

    Sys.Process("Explorer").Window("Shell_TrayWnd").Activate()

    VBScript

    Sys.Process("Explorer").Window("Shell_TrayWnd").Activate

    DelphiScript

    Sys.Process('Explorer').Window('Shell_TrayWnd').Activate;

    C++Script, C#Script

    Sys["Process"]("Explorer")["Window"]("Shell_TrayWnd")["Activate"]();

    Another solution is to launch the desired Office application and work with it only using its OLE object. However, in this case you may not notice changes made to the application, because launched in this way, it is not displayed on screen initially. To make the application visible, can set the Visible property of its OLE object to True:

    JavaScript, JScript

    // Launch Excel
    var Excel = Sys.OleObject("Excel.Application");
    // Make it visible
    Excel.Visible = true;
    ...

    Python

    # Launch Excel
    Excel = Sys.OleObject["Excel.Application"]
    # Make it visible
    Excel.Visible = True
    # ...

    VBScript

    Dim Excel
    ' Launch Excel
    Set Excel = Sys.OleObject("Excel.Application")
    ' Make it visible
    Excel.Visible = True
    ...

    DelphiScript

    var Excel : OleVariant;
    ...
    // Launch Excel
    Excel := Sys.OleObject['Excel.Application'];
    // Make it visible
    Excel.Visible := true;
    ...

    C++Script, C#Script

    // Launch Excel
    var Excel = Sys["OleObject"]("Excel.Application");
    // Make it visible
    Excel["Visible"] = true;
    ...

  • As an alternative to Sys.OleObject, you can use the getActiveXObject method which is specific to JavaScript (not JScript). It significantly improves performance of OLE object scripting.

Example

The following example demonstrates how to use OleObject property in scripts:

JavaScript

function ReadDataFromExcel()
{
  let Excel = Sys.OleObject("Excel.Application");
  Excel.Workbooks.Open("C:\\MyFile.xlsx");

  let RowCount = Excel.ActiveSheet.UsedRange.Rows.Count;
  let ColumnCount = Excel.ActiveSheet.UsedRange.Columns.Count;

  for (let i = 1; i <= RowCount; i++)
  {
    let s = "";
    for (let j = 1; j <= ColumnCount; j++)
      s += (VarToString(Excel.Cells.Item(i, j)) + "\r\n");
    Log.Message("Row: " + i, s);
  }

  Excel.Quit();
}

JScript

function ReadDataFromExcel()
{
  var Excel = Sys.OleObject("Excel.Application");
  Excel.Workbooks.Open("C:\\MyFile.xlsx");

  var RowCount = Excel.ActiveSheet.UsedRange.Rows.Count;
  var ColumnCount = Excel.ActiveSheet.UsedRange.Columns.Count;

  for (var i = 1; i <= RowCount; i++)
  {
    var s = "";
    for (var j = 1; j <= ColumnCount; j++)
      s += (VarToString(Excel.Cells(i, j)) + "\r\n");
    Log.Message("Row: " + i, s);
  }

  Excel.Quit();
}

Python

def ReadDataFromExcel():
  Excel = Sys.OleObject["Excel.Application"]
  Excel.Workbooks.Open("C:\\MyFile.xlsx")

  RowCount = Excel.ActiveSheet.UsedRange.Rows.Count
  ColumnCount = Excel.ActiveSheet.UsedRange.Columns.Count

  for i in range(1, RowCount + 1):
    s = "";
    for j in range(1, ColumnCount + 1):
      s = s + VarToString(Excel.Cells.Item[i, j]) + '\r\n'
    Log.Message("Row: " + VarToString(i), s);

  Excel.Quit();

VBScript

Sub ReadDataFromExcel
  Dim Excel, RowCount, ColumnCount, i, j, s

  Set Excel = Sys.OleObject("Excel.Application")
  Excel.Workbooks.Open("C:\MyFile.xlsx")

  RowCount = Excel.ActiveSheet.UsedRange.Rows.Count
  ColumnCount = Excel.ActiveSheet.UsedRange.Columns.Count

  For i = 1 To RowCount
    s = ""
    For j = 1 To ColumnCount
      s = s & VarToString(Excel.Cells(i, j)) & vbNewLine
    Next
    Log.Message "Row: " & i, s
  Next

  Excel.Quit
End Sub

DelphiScript

procedure ReadDataFromExcel;
var Excel, RowCount, ColumnCount, i, j, s;
begin
  Excel := Sys.OleObject('Excel.Application');
  Excel.Workbooks.Open('C:\MyFile.xlsx');

  RowCount := Excel.ActiveSheet.UsedRange.Rows.Count;
  ColumnCount := Excel.ActiveSheet.UsedRange.Columns.Count;

  for i := 1 to RowCount do
  begin
    s := '';
    for j := 1 to ColumnCount do
      s := s + VarToString(Excel.Cells(i, j)) + #13#10;
    Log.Message('Row: ' + VarToString(i), s);
  end;

  Excel.Quit;
end;

C++Script, C#Script

function ReadDataFromExcel()
{
  var Excel = Sys["OleObject"]("Excel.Application");
  Excel["Workbooks"]["Open"]("C:\\MyFile.xlsx");

  var RowCount = Excel["ActiveSheet"]["UsedRange"]["Rows"]["Count"];
  var ColumnCount = Excel["ActiveSheet"]["UsedRange"]["Columns"]["Count"];

  for (var i = 1; i <= RowCount; i++)
  {
    var s = "";
    for (var j = 1; j <= ColumnCount; j++)
      s += (VarToString(Excel["Cells"](i, j)) + "\r\n");
    Log["Message"]("Row: " + i, s);
  }

  Excel["Quit"]();
}

See Also

Viewing COM Object Properties, Methods and Events
GetRemoteOLEObject Method

Highlight search results