传递给参数“作为变体"的选定 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++
问题描述
这个问题来自以下问题:
This question arose from the following question :
假设我有一个暴露给 vba 的 dll 的 ATL/COM 类 TheClass
的实例 MyObj1
,以及一个方法 TheMethod
TheClass
.
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
.
在 VBA 中,我有一个函数
In VBA, I have a function
Public Function func(x as Variant) as Variant
'... something
MyObj1.TheMethod( x )
'... other things
End Function
这个函数用在excel中,我传递了一个excel范围的单元格给它.在 VBA 中调试时,x 的 vba 类型为Variant/Object/Range
",并且具有 vba 类型Variant/Variant<"的
Value2
"/code>",如果范围是例如一个包含 expty 单元格的矩形,在 excel 中是双倍的,当 Value2
展开时(在 vba 的调试中仍在观察),它由 组成Variant
类型为Variant/Double
"或Variant/Empty
".我想在下一步中隔离的正是这个核心"Value2
"Variant
,在 c++ 中.
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++.
下一步,在debug in c++的ATL方法入口处TheMethod
签名
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
(接收到后一个x
)的vt
等于VT_DISPATCH
.这家伙肯定(即使我不知道)携带了后者 x
已经携带的所有东西,但我想知道如何隔离Value2
" 它的一部分是捕捉它并使用它,因为只有 Value2
部分让我感兴趣.
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.
让我说我知道我可以在 vba 中做到这一点,而不是在 C++ 中进行:
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
但是我不想在 VBA 中使用这种东西.
推荐答案
好的,正如 Igor Tandetnik 所建议的,我做了类似的事情:
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);
将真正的数组变体"(vt 等于 VT_ARRAY)打包到 VT_DISPATCH 变体 pVARIANT_IN 中.
to get the real "array variant" (of vt equal to VT_ARRAY) packed into the VT_DISPATCH variant pVARIANT_IN.
我稍后会尝试导入Excel的类型库.
I will later give a try to the import of Excel's type library.
这篇关于传递给参数“作为变体"的选定 Excel 单元格范围VBA 函数,然后传递给 C++ 中 ATL 对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!