In this article:

General Information

Working with COM Objects on Form

Examples of Interaction with Various Applications

General Guidelines for Working with COM Objects from Fore

Working with COM Objects from Fore

Article number: KB000032

General Information

The COM standard is developed by Microsoft for creating software based on interacting components, each of which can be used for many applications simultaneously. In the Fore language, to create COM objects and enable interaction between them, use methods of Variant class.

To create an instance of the COM object, call the CreateObject  method specifying the program identifier of the application. If the application is running, it can be accessed using the  GetActiveObject:

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

NOTE. The program identifier of the application can be obtained from the operating system registry. For more information, see in the description of the following methods:  CreateObject, GetActiveObject.

To enable interaction with the obtained instance of the application, use the following method:  GetProperty, GetPropertyEx, SetProperty, SetPropertyEx, Invoke. To work with the obtained instance of the COM object, study its API first.

Working with COM Objects on Form

To work with COM objects in a user application form the development environment, use the  OleDocumentBox component. The  CreateObject component method allows to create a new instance of the COM object, and the CreateFromFile method creates the COM object according to the specified file type and then loads the specified file.

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

Then, using the  OleObject property allows to work with the created instance using the above specified methods of the Variant data type:

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

Examples of Interaction with Various Applications

The following sections contain examples of interaction with the most popular applications and troubleshooting options when working with Fore applications:

Examples of interaction with applications

General Guidelines for Working with COM Objects from Fore

If the COM object properties and methods in the original API use any enumerated types, to use them from Fore, specify numeric values that correspond to the corresponding enumeration elements. To read the code generated by Fore and avoid overloading the code with numbers, use similar enumerations for defining or checking property values or for choosing 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 enumerated types declared in the COM object API. To execute this method, the Fore code 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;

To determine what the values 1,2,3 correspond to and what happens as a result of executing this method is difficult without additional comments. Thus, modify the code by defining the UserEnum1, UserEnum2, UserEnum3 enumerations in Fore. The values of enumeration elements must correspond to the values in the original API of the 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:

Developers Knowledge Base