Delphi – How to Test if olevariant contains interfaces

An annoying thing about using Excel through its automated interface is weak typing.
The return value can contain any different types.
How to test whether the variable returned by the caller Interface for ExcelRange?

function TAddInModule.InBank: boolean;
var
ExcelAppAsVariant: OleVariant;
test: string;
Caller : OleVariant;
begin //Exception handling omitted for brevity.
Result:= false;
ExcelAppAsVariant:= ExcelApp.Application;
Caller:= ExcelApp.Application.Caller[EmptyParam , 0];
if IDispatch(Caller) is ExcelRange then begin //E2015 Operator not applicable
Result:= lowercase(Caller.Parent.Name) ='bank'
end;
end;

(Ironically is the working principle of the as operator (IDispatch(Caller) as ExcelRange).Parent; compiles well).

The following code works, but It seems too verbose:

if VarIsType(Caller, varDispatch) then begin 
IUnknown(Caller).QueryInterface(ExcelRange, ICaller)
if Assigned(ICaller ) then ICaller......

There is no built-in function VarIsInterface(Variant,Interface).
How to test whether OleVariant contains a given interface?

See also: How to cast OleVariant to IDispatch derived?

Edit
Thank you, I use the following method to test, because Excel mixes the interface and OleStrings into possible returns Value.

if VarIsType(Caller, varDispatch) and Supports(Caller, ExcelRange) then begin

I might use Supports:

if Supports(Caller, ExcelRange) then 
....

This resolves to the same code given by @Stijn, but the Supports call is more concise.

By its automation One of the annoying things about using Excel in the interface is the weak type.
The return value can contain any different types.
How to test whether the variable returned by the caller is the ExcelRange interface?

function TAddInModule.InBank: boolean;
var
ExcelAppAsVariant: OleVariant;
test: string;
Caller : OleVariant;
begin //Exception handling omitted for brevity.
Result:= false;
ExcelAppAsVariant:= ExcelApp.Application;
Caller:= ExcelApp.Application.Caller[EmptyParam , 0];
if IDispatch(Caller) is ExcelRange then begin //E2015 Operator not applicable
Result:= lowercase(Caller.Parent.Name) ='bank'
end;
end;

(Ironically is the working principle of the as operator (IDispatch(Caller) as ExcelRange).Parent; compiles well).

The following code works, but It seems too verbose:

if VarIsType(Caller, varDispatch) then begin 
IUnknown(Caller).QueryInterface(ExcelRange, ICaller)
if Assigned(ICaller ) then ICaller......

There is no built-in function VarIsInterface(Variant,Interface).
How to test whether OleVariant contains a given interface?

See also: How to cast OleVariant to IDispatch derived?

Edit
Thank you, I use the following method to test, because Excel mixes the interface and OleStrings into possible returns Value.

if VarIsType(Caller, varDispatch) and Supports(Caller, ExcelRange) then begin

I might use Supports:

if Supports(Caller, ExcelRange) then
....

This resolves to the same code given by @Stijn, but the Supports call is more concise.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3137 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.