比较VBA中COM对象的标识

时间:2016-10-15 17:43:53

标签: vba com identity

在C ++中如果我想知道两个COM接口指针p1和p2是否指向同一个组件,我会这样做:

bool IsSame( IDispatch* p1, IDispatch* p2 )
{
    IUnknown* pUnk1;
    p1->QueryInterface( IID_IUnknown, reinterpret_cast<void**>(&pUnk1) );
    IUnknown* pUnk2;
    p2->QueryInterface( IID_IUnknown, reinterpret_cast<void**>(&pUnk2) );
    return p1 == p2;
}

如何在VBA中执行此操作?我想将它用于两个MSForms.ListBox对象:

Private Function IsSame( p1 as MSForms.ListBox, p2 as MSForms.ListBox )
    ' Comparing p1 and p2 would compare their default Property, which is their Value
End Function

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Is来比较两个对象:

Private Function IsSame(p1 As msforms.ListBox, p2 As msforms.ListBox)
IsSame = p1 Is p2
End Function