Methods, property-get functions and property-put functions are called using the following syntax:
[... = ]ip.method[(arguments)]
[... = ]ip.property[(arguments)]
ip.property[(arguments)] = value
Here ip is interface pointer variable; method and property are names of functions. Arguments often have BSTR or VARIANT type, but you can pass values of QM intrinsic types (str, etc) instead. QM converts them automatically.
Most COM functions actually return success code, which is 0 on success, negative on error, positive on partial success or if function returns some information. In C++ it is defined as integer of type HRESULT (int in QM). You don't use it when calling function (but you can access it through special variable _hresult). Instead, as return value is used the last argument if in type library it is marked as return value. If function returns negative HRESULT, QM generates error, which can be handled (err). This behavior is only if function's return value is declared as HRESULT (near all functions are declared in this way).
A single statement can contain more than one function call. If a function returns interface pointer, you can immediately call a function on that interface pointer. For example, statement:
Excel.Worksheet xlSheet=+xlApp.Workbooks.Add(xlWBATWorksheet).ActiveSheet
does the same job as the following three statements:
Excel.Workbooks xlBooks=xlApp.Workbooks Excel.Workbook xlBook=xlBooks.Add(xlWBATWorksheet) Excel.Worksheet xlSheet=+xlBook.ActiveSheet
QM 2.3.0: This also can be used with interfaces declared using interface.
If argument must be passed by reference (& in the status bar), you must pass variable of that type, or address of variable of that type. You cannot pass simple number or string as you could do in Visual Basic. For example, VB statement
doc = Documents.Add("file")
would be the same in QM, but only if argument is passed by value (e.g., declared as VARIANT'filename). If argument is passed by reference (e.g., declared as VARIANT&filename), it should be
VARIANT v="file"; doc = docs.Add(v) or VARIANT v="file"; doc = docs.Add(&v)
QM does not support default members. For example, Visual Basic statement
doc = docs(1) ;;get item #1 from collection
in QM could be
doc = docs.Item(1)
In the popup list of functions default member is blue.
QM partially supports optional arguments. Optional arguments are enclosed in [ ] in the status bar. If optional argument has VARIANT type and does not have default value, you can omit it. Otherwise, you can use value that is shown in the status bar in square brackets after argument definition and =. If optional argument is followed by argument that you use, place @ instead of optional argument. In the following example is shown how to call function Func, which has three arguments, all optional, but you use only second argument:
ip.Func(@ arg2)
If function supports variable number of arguments, function definition in the status bar is followed by "vararg". If you use vtbl binding, store arguments into ARRAY(VARIANT) and pass it as last argument. Example:
Assume that the status bar shows this: Func(BSTR's ARRAY(VARIANT)*a) . vararg
ARRAY(VARIANT) a.create(2) a[0] = 1; a[1] = "string2" ip.Func("string1" &a)
If you use IDispatch::Invoke (e.g., #opt dispatch 1 is set, or interface does not support vtbl binding), pass variable number of arguments instead of ARRAY:
ip.Func("string1" 1 "string2")
You can also call functions of undeclared interfaces. Interface pointer variable must be of IDispatch type (this is similar to the Object type in Visual Basic). Example:
IDispatch app._create("Word.Application") app.Visible = -1
Arguments are not declared, and therefore argument types should match the expected types (however, it often isn't necessary). QM does implicit conversions only where it is obvious, such as str to BSTR. The type of the return value is always VARIANT.
QM cannot show type info (popup list of functions, function definition in the status bar) for undeclared interfaces.
You can also use VBScript or JScript code in QM. Example:
lpstr code= Set app=CreateObject("Word.Application") app.Visible=True VbsExec code
Or, parts of code that manipulates single object can be in VBScript, and other parts in QM. Functions VbsFuns and VbsEval can be used to exchange values between VBScript and QM.
See other topics and COM samples in the forum.