Working with COM-Objects in Fore

The COM standard was introduced by Microsoft and it is used to create a software based on communication objects, each component can be used in a big number of programs in parallel. In the Fore language, to create and communicate with COM-objects, methods of the Variant class are used.

To create a COM-object instance, it is required to call the CreateObject method specifying the application program identifier. If the application is started, it can be accessed via the GetActiveObject method:

Var
    NewInstance, ExistInstance: Variant;
Begin
    NewInstance := Variant.CreateObject(<ProgID>);
    ExistInstance := Variant.GetActiveObject(<ProgID>);

NOTE. Application program identifier can be obtained from operating system registry, for details see description of the CreateObject, GetActiveObject methods.

Communication with obtained application instance is executed using the GetProperty, GetPropertyEx, SetProperty, SetPropertyEx, Invoke methods. To work with obtained instance of COM-object it is required to previously study its API.

Working with COM-Objects on Form

To work with COM-objects in the desktop application on application form in development environment, the OleDocumentBox component is used. The CreateObject components method enables creating a new instance of COM-object and the CreateFromFile method creates a COM-object according to the type of specified file, after that it loads the specified file.

NOTE. In Foresight Analytics Platform the CreateFromFile method is supported only for files Microsoft Excel and Microsoft Word.

Next, using the OleObject property, one can work with the created instance using above mentioned methods of the Variant data type:

Var
    Obj: Variant;
Begin
    OleDocumentBox1.CreateObject(<ProgID>);
    Obj := OleDocumentBox1.OleObject;

Examples of Communication with Various Applications

The following sections give examples how to communicate with the most common applications and how to solve various problems on working with applications from Fore:

Example how to communicate with applications

Working with Microsoft Excel

General Recommendations How to Work with COM-Objects from Fore

If COM-object properties or methods use any enumerable types in the source API, then to use them from Fore it will be necessary to specify numeric values that match corresponding enumeration elements. In order that a code created in Fore will be readable and not overloaded with only numbers, it is recommended to declare identical enumerations and use them on setting or checking properties values or on executing methods.

Example:

There is a COM-object for which the Run method is implemented with the following signature:

Function Run(a: UserEnum1, b: UserEnum2): UserEnum3;

UserEnum1, UserEnum2, UserEnum3 are enumerable types declared in in COM-object API. To execute this method, a code in Fore may look as follows:

Sub UserProc;
Var
    v, Res: Variant;
Begin
    v := Variant.CreateObject(<ProgID>);
    Res := v.Invoke("Run"31);
    Select Case (Res As Integer)
        Case 1//...
        Case 2//...
    End Select;
End Sub UserProc;

With no additional comments it is difficult to determine to what correspond the 1,2,3 values and what will be as a result of the method execution, that is why modify the code by defining the UserEnum1, UserEnum2, UserEnum3 enumerations in Fore. Values of enumeration elements must correspond to values in the source API of COM-object:

Enum UserEnum1
    a, b, c
End Enum UserEnum1;
Enum UserEnum2
    a = 1, b = 2, c = 3
End Enum UserEnum2;
Enum UserEnum3
    a = 1, b = 2, c = 4
End Enum UserEnum3;

Sub UserProc;
Var
    v, Res: Variant;
Begin
    v := Variant.CreateObject(<ProgID>);
    Res := v.Invoke("Run", UserEnum2.c, UserEnum1.a);
    Select Case (Res As UserEnum3)
        Case UserEnum3.a: //...
        Case UserEnum3.b: //...
    End Select;
End Sub UserProc;

See also:

Developing User Application