1

How do I pass an array of a user defined class type ByRef from excel vba to vb.net? I have a similar post that deals with passing double arrays here and I tried using the same method with my UDT but it doesn't work. Any ideas?

4

1 回答 1

1

很抱歉(再次)回答我自己的问题,该问题与我在这个问题中链接的另一个问题非常相似,但我能够以与另一个问题的答案几乎相同的方式完成它。我只需要改变一些折射参数。

在 vb.net 代码中,我将参数作为对象接收,获取该对象的类型(它识别为 object() 数组),然后使用 vb.net 中的以下函数将该对象数组直接转换为所需的类数组

Friend Function ComObjectArrayToPointArray(ByVal comObject As Object) As Point()
    Dim thisType As Type = comObject.GetType
    Dim fibType As Type = Type.GetType("System.Object[]")
    Dim fibArray(0) As Point
    If thisType Is fibType Then
        Dim args(0) As Object
        Dim numEntries As Integer = CInt(thisType.InvokeMember("Length", BindingFlags.GetProperty, _
                                        Nothing, comObject, Nothing))
        ReDim fibArray(numEntries - 1)
        For j As Integer = 0 To numEntries - 1
            args(0) = j
            fibArray(j) = DirectCast((thisType.InvokeMember("GetValue", BindingFlags.InvokeMethod, _
                                    Nothing, comObject, args)), Point)
        Next
    End If

    Return fibArray

End Function
于 2013-08-07T19:18:41.513 回答