Variant.Invoke

Syntax

Invoke(Name: String; Parameters: Array): Variant;

Parameters

Name. Name of the method that must be executed for the current object.

Parameters. The array of parameters values required to execute the selected method. If the executed method has no parameters, this parameter may be omitted.

Description

The Invoke method executes the method, which name is passed by the Name input parameter.

Comments

The method name and parameters depend on the object, for which they are applied.

Values of the parameters used for method execution can be given as a separate array or listed separated by commas. Array size must match the number of passed parameters.

It is necessary to use an array to execute methods that have referenced parameters in their signature. The values obtained as a result of method execution are saved to appropriate array values. Before the method is executed, array elements to which the values are to be saved, should be initialized in accordance with method parameters' data type.

Important. Parameter values should be passed to the Invoke method in reverse order relative to the order of parameters in the signature of executed method.
Values of optional parameters can be omitted. If a value of certain optional parameter should be specified, it is also necessary to specify values of all preceding mandatory and optional parameters.

Example: The executed method has the following signature MySub(A, B, C: Variant). Invocation of the Invoke method must look as follows:
Var
    //variable list
    v: Variant;
Begin
    v := //Initialize object
    ...
    v.Invoke("MySub", C, B, A);

or:

Var
    //variables list
    v: Variant;
    Arr: Array Of Variant;
Begin
    v := //Initialize object
    ...
    Arr := New Variant[3];
    Arr[0] := C;
    Arr[1] := B;
    Arr[2] := A;
    v.Invoke("MySub", Arr);

Where v is the object, for which the method is executed. A,B,C are method parameters, Arr is the array containing method parameter values.

Example: The executed method has the following signature: MySub(A,B: Double; Var C: Double). The Invoke method must be called as follows:

Var
    //variables list
    v: Variant;
    Arr: Array Of Variant;
Begin
    v := //Initialize object
    ...
    Arr := New Variant[3];
    Arr[0] := Double.MinValue;
    Arr[1] := B;
    Arr[2] := A;
    v.Invoke("MySub", Arr);

Where v is the object, for which the method is executed. A,B are values of method parameters (the values must be Double), Arr is the array containing method parameters' values. After executing the method the Arr[0] element shows calculated value corresponding to the method parameter C.

Example 1

Executing the example requires a form with the Button1 button, and the OleDocumentBox component named OleDocumentBox1.

Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
    Excel, Sheet, OleObj, ComButton: Variant;
    Arr: Array[11];
Begin
    OleDocumentBox1.CreateObject("Excel.Sheet.8");
    Excel := OleDocumentBox1.OleObject.GetProperty("Application");
    Sheet := Excel.GetProperty("ActiveSheet");
    OleObj := Sheet.GetProperty("OLEObjects");
    Arr[0] := 20//Height
    Arr[1] := 100//Width
    Arr[2] := 10//Top
    Arr[3] := 10//Left
    Arr[4] := -1//IconLabel
    Arr[5] := -1//IconIndex
    Arr[6] := ""//IconFileName
    Arr[7] := False//DisplayAsIcon
    Arr[8] := False//Link
    Arr[9] := ""//FileName
    Arr[10] := "Forms.CommandButton.1"//ClassType
    ComButton := OleObj.Invoke("Add", Arr);
    ComButton.GetProperty("Object").SetProperty("Caption""Start");
End Sub Button1OnClick;

Clicking the button creates a new Microsoft Excel sheet and loads it to the OleDocumentBox1 component. A control (button) is created on this sheet. To create a control, execute the Add method of OLEObjects collection with the following VBA syntax:

Add(ClassType, FileName, Link, DisplayAsIcon, IconFileName, IconIndex, IconLabel, Left, Top, Width, Height)

When the Invoke method is executed, the parameter values for the Add method are passed in the reverse order.

Example 2

Executing the example requires a form with the Button1 button, and the OleDocumentBox component named OleDocumentBox1. A Microsoft Excel document is loaded to the OleDocumentBox1 component.

Enum XlFixedFormatType
    xlTypePDF, xlTypeXPS
End Enum XlFixedFormatType;

Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
    Excel, Sheet: Variant;
Begin
    Excel := OleDocumentBox1.OleObject;
    Sheet := Excel.GetProperty("ActiveSheet");
    Sheet.Invoke("ExportAsFixedFormat""c:\Data.Pdf", XlFixedFormatType.xlTypePDF);
End Sub Button1OnClick;

Clicking the button exports all pages of the active document sheet to PDF. The ExportAsFixedFormat method is used for export, with the following VBA syntax:

ExportAsFixedFormat(

Type,

Filename,

Quality,

IncludeDocProperties,

IgnorePrintAreas,

From,

To,

OpenAfterPublish,

FixedFormatExtClassPtr);

The Type parameter is mandatory, the XlFixedFormatType enumeration is used to set its value. This enumeration is a Fore analog of the VBA enumeration XlFixedFormatType. All the other parameters are optional. Only the second parameter is specified, that is, name of the file, to which the data is exported.

When executing the Invoke method, parameter values for the ExportAsFixedFormat method are passed in reverse order.

See also:

Variant