传递到参数“as Variant”的选定范围的Excel单元格的VBA函数,然后传递给c ++中的ATL对象的方法 [英] Selected range of Excel cells passed to an argument "as Variant" of a VBA function, then passed to an method of an ATL object in c++
问题描述
这个问题来自以下问题:
假设我有一个实例 MyObj1
暴露给vba的dll的一个ATL / COM类 TheClass
和一个方法 TheMethod
of TheClass
。
在VBA中,我有一个函数
public function func(x as Variant)as Variant
'... something
MyObj1.TheMethod(x)
'...其他事物
结束函数
这个函数用于excel,我传递一个excel范围的单元格。在VBA中调试时,x看到vba类型 Variant / Object / Range
,并具有 Value2
of vba type Variant / Variant
,如果范围是例如一个包含excel中的单元格和双精度的矩形,当 Value2
展开(仍然在vba调试中观察),它由
变体
的类型 Variant / Double
或 Variant / Empty
。正是这个核心 Value2
变体
,我想在下一步隔离,
在下一步,在c ++中在ATL方法 TheMethod
of signature
STDMETHODIMP TheClass :: TheMethod(VARIANT * var_in)
var_in
(接收后者 x
)具有等于 VT_DISPATCH
的 vt
。这个人确实(即使我不知道)所有的东西已经由后者 x
,但我想知道如何隔离 Value2
部分感兴趣我。
code> public function func(x as Variant)as Variant
'... something
MyObj1.TheMethod(x.Value2)
'...其他东西
结束函数
但我不想诉诸VBA中的这样的东西
好的,Igor Tandetnik建议,我做了一些:
VARIANT * pVARIANT_IN_EXTENSION = new VARIANT();
VariableantInit(pVARIANT_IN_EXTENSION);
IDispatch * piDisp = pVARIANT_IN-> pdispVal;
:: DISPPARAMS invokeparams = {0};
HRESULT hRes = piDisp-> Invoke(0,IID_NULL,0,DISPATCH_PROPERTYGET,& invokeparams,pVARIANT_IN_EXTENSION,NULL,NULL); $ v
以获取真正的数组变量(vt等于VT_ARRAY),并打包到VT_DISPATCH变量pVARIANT_IN 。
稍后我将尝试导入Excel的类型库。
This question arose from the following question :
Chaining methods of ATL/COM objects
Let's say I have a instance MyObj1
of an ATL/COM class TheClass
of a dll exposed to vba, and a method TheMethod
of TheClass
.
In VBA, I have a function
Public Function func(x as Variant) as Variant
'... something
MyObj1.TheMethod( x )
'... other things
End Function
This function is used in excel, and I pass an excel range of cell to it. At debug in VBA, x is seen of vba type "Variant/Object/Range
", and has a "Value2
" of vba type "Variant/Variant
", and if the range is for instance a rectangle containing expty cells and doubles in excel, when Value2
is unfolded (still in watch in debug in vba), it is composed of Variant
's of types "Variant/Double
" or "Variant/Empty
". It is this "core" "Value2
" Variant
that I would like to isolate in the next step, in c++.
At the next step, at debug in c++ at the entry of the ATL method TheMethod
of signature
STDMETHODIMP TheClass::TheMethod( VARIANT* var_in )
var_in
(which received the latter x
) is seen having a vt
equal to VT_DISPATCH
. This guy carries for sure (even if I don't know) all the stuff already carried by the latter x
, but I would like to know how to isolate the "Value2
" part of it to catch it and work with it, as only the Value2
part interests me.
Let me say that I know that rather than doing it in c++, I could do this in vba :
Public Function func(x as Variant) as Variant
'... something
MyObj1.TheMethod( x.Value2 )
'... other things
End Function
but I don't want to resort to such thing in VBA.
Ok, so as Igor Tandetnik suggested, i did something like :
VARIANT * pVARIANT_IN_EXTENSION = new VARIANT() ;
VariantInit(pVARIANT_IN_EXTENSION);
IDispatch * piDisp = pVARIANT_IN->pdispVal ;
::DISPPARAMS invokeparams = {0} ;
HRESULT hRes = piDisp->Invoke(0, IID_NULL, 0, DISPATCH_PROPERTYGET,&invokeparams, pVARIANT_IN_EXTENSION, NULL, NULL);
to get the real "array variant" (of vt equal to VT_ARRAY) packed into the VT_DISPATCH variant pVARIANT_IN.
I will later give a try to the import of Excel's type library.
这篇关于传递到参数“as Variant”的选定范围的Excel单元格的VBA函数,然后传递给c ++中的ATL对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!