createComObject()

Creates a COM object (also without user interface (not ActiveX)).

Synopsis

idispatch createComObject(string objName [, bool bWithEvents, string postfix]);

Parameters

Parameter Description
objName Name of the COM Object, for example, "Excel.Application"; or "ADODB.Connection";
bwithEvents if TRUE was set, COM object responds to events (like mouse click etc).
postfix Defines the COM object, which should respond to an event in case of several COM objects.

Return value

In case of errors 0.

Errors

Undefined functions, missing or wrong parameters.

Description

Creates an arbitrary COM object. This function can be used many-sided. It can be used to control and fill databases as well as to control Excel and Word applications etc. The properties that can be controlled via the function can be detected via an external browser ( for example, Visual Basic). Since COM objects are Windows specific, the function is only available under Windows.

The UI always connects to all event interfaces of a COM object, so if a COM object contains multiple interfaces with the same event, it can not be distinguished which event has taken place.

Note: Note that the COM control for controlling MS Office is not recommendable. Alternatively use the Microsoft solution - see "Alternatives to server-side Automation" under https://support.microsoft.com/en-us/kb/257757.

In order to avoid performance problems the "." operator on idispatch should be used as rare as possible and COM objects should not be fetched more than once (saved in idispatch).

Catching Events via COM

In the Control Manager the COM Object can call a callback function, when the bwithEvents parameter is set to TRUE. The callback functions are called as defined by the COM Object, e.g.:

void onEventInitialize_myPostfix(...){ }

whereas "_myPostfix" is the optional third parameter of the createComObject() function.

With -dbg 2 all possible Events are shown (in Qt known as "signals") which are incoming after the call of createComObject(). Qt data types are returned, i.e. the CTRL functions must be defined with modified syntax.

Events do not support "by reference" arguments.

main()
{
  idispatch objXL;
  idispatch objXLWorkbooks;
  idispatch objXLWorkbook;
  anytype workbook;
  objXL = createComObject("Excel.Application", true, "TEST");
  objXLWorkbooks = objXL.Workbooks;
  objXLWorkbook = objXLWorkbooks.Add(workbook);
}

void NewWorkbook_TEST()
{
  DebugN("NEW Workbook");
}

Create a button using the graphics editor and use the code below for the button. The code opens an Excel sheet and afterwards a diagram, that rotates:

main()
{
  idispatch objXL;
  idispatch objXLchart;
  idispatch objXLWorkbooks;
  idispatch objXLWorkbook;
  int i;
  dyn_int array;
  anytype workbook;
  array = makeDynInt(1,2,3);
  objXL = createComObject("Excel.Application");
  objXL.Visible = -1;
  objXLWorkbooks = objXL.Workbooks;
  objXLWorkbook = objXLWorkbooks.Add(workbook);
  releaseComObject(objXLWorkbook);
  releaseComObject(objXLWorkbooks);
  objXL.Range("A1:C1", workbook).Value2 = array;
  objXL.Range("A1:C1", workbook).Select();
  objXLchart = objXL.Charts.Add(workbook, workbook, workbook, workbook);
  objXLchart.Visible = -1;
  objXLchart.Type = -4100;

  //xl3DColumn
  //xl3DColumn is an Excel-defined constant with value = -4100.
  for (i = 30; i<=180; i = i + 10)
  {
    delay(0,800);
    objXLchart.Rotation = i;
  }

  objXL.Visible = 0;
  releaseComObject(objXLchart);
  releaseComObject(objXL);
  delay(20);
}

This example opens a connection to an Ado database, and executes a query. Since two COM objects are created, you have to define the postfix "rs".

main()
{
 cnn = createComObject( "ADODB.Connection");
 cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51; Data
Source=D://TEMP//books.mdb;"
 cnn.Open("", "", "",  -1); // open a new connection
 rs = createComObject( "ADODB.Recordset", true, "rs" );
 rs.CursorLocation = 3;
 //position of the cursor
  rs.Open ("SELECT * FROM Booksales ", cnn, 2 /*adOpenDynamic*/, -1, -1 );
}

Assignment

Miscellaneous functions

Availability

UI