excel - 将数组从 VBA 传递到 VB.NET

标签 excel vba vb.net com excel-interop

我正在开发一个在 Microsoft Excel 中工作的 vb.net COM 互操作,并且在将数组从 vb 传递到 vb.net 时遇到问题。我有一个 PointPairs我需要从 vb 设置的 vb.net 代码中的属性,并且在传递二维数组时遇到问题。我已经尝试使用 2D 数组显式设置属性以及将两个 1D 数组传递到 Sub 以尝试在 vb.net 中设置属性,但我尝试过的任何方法似乎都不起作用。

vb.net 代码:

Public Property PointPairs() As Double(,)
   Get
     ...
     Return array
   End Get
   Set(ByVal Value(,) As Double)
     ...
   End Set
End Property

Public Sub SetPointPairs(ByRef spline As Spline, ByRef xValues() As Double, _
                              ByRef yValues() As Double)
   Dim Value(,) As Double
   ReDim Value(1, UBound(xValues, 1))

   For i As Integer = 0 To UBound(xValues, 1)
       Value(0, i) = xValues(i)
       Value(1, i) = yValues(i)
   Next

   spline.PointPairs = Value
End Sub

VB代码:
Dim spline1 As New Spline
Dim points(), xValues(), yValues() As Double
'read input from excel cells into points() array/add x and y values to respective arrays

spline1.PointPairs = points 'first method (doesn't work)
Call SetPointPairs(spline1, xValues, yValues)  'second method (doesn't work)

vb.net 正确导出了所有内容,并且属性/子程序/函数在 vba 的对象浏览器中可见,但是当我尝试以这两种方法传递数组时,我收到错误消息 Function or interfaces markes as restricted, or the function uses an automation type not supported in Visual BasicSub or Function not defined .我也尝试过使用 <MarshalAs()>但我以前从未使用过它,也找不到太多关于如何使用它在 vb 和 vb.net 之间传递数组的文档。

提前感谢您的任何建议或解决方案

最佳答案

对于任何对该解决方案感兴趣的人,我发现这篇文章正是我所需要的。

http://www.codeproject.com/Articles/12386/Sending-an-array-of-doubles-from-Excel-VBA-to-C-us?fid=247508&select=2478365&tid=2478365

我不得不在 VBA 中将 2D 数组分解为两个 Doubles 的 1D 数组,并将它们作为对象传递到 vb.net 中,并按照文章中的说明对其进行修改。我按如下方式更改了 SetPointPairs Sub,并添加了此私有(private)函数以在 .net 代码中从 Object 转换为 Array

Sub SetPointPairs(ByRef spline As CubicSpline, ByRef xValues As Object, ByRef yValues As Object) Implements iCubicSpline.SetPointPairs

        Dim xDbls(), yDbls(), pointDbls(,) As Double

        xDbls = ComObjectToDoubleArray(xValues)
        yDbls = ComObjectToDoubleArray(yValues)
        ReDim pointDbls(1, UBound(xDbls, 1))
        For i As Integer = 0 To UBound(pointDbls, 2)
            pointDbls(0, i) = xDbls(i)
            pointDbls(1, i) = yDbls(i)
        Next

        spline.PointPairs = pointDbls

End Sub

Private Function ComObjectToDoubleArray(ByVal comObject As Object) As Double()

        Dim thisType As Type = comObject.GetType
        Dim dblType As Type = Type.GetType("System.Double[]")
        Dim dblArray(0) As Double

        If thisType Is dblType Then
            Dim args(0) As Object
            Dim numEntries As Integer = CInt(thisType.InvokeMember("Length", BindingFlags.GetProperty, _
                                            Nothing, comObject, Nothing))
            ReDim dblArray(numEntries - 1)
            For j As Integer = 0 To numEntries - 1
                args(0) = j
                dblArray(j) = CDbl(thisType.InvokeMember("GetValue", BindingFlags.InvokeMethod, _
                                        Nothing, comObject, args))
            Next
        End If

        Return dblArray

    End Function

关于excel - 将数组从 VBA 传递到 VB.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17552107/

相关文章:

c# - 连接的TCP套接字如何同时发送/接收多个文件?

c# - 如何以编程方式检索 Excel 工作表中的页数?

Excel:在与另一列匹配的列中查找最小/最大值

VBA UDF 使用动态数组查找多列条件匹配

c# - 检测 ctrl+左键单击 winforms 应用程序

arrays - 调整图像大小并放置在 CrystalReports BlobField 的字节数组中

c# - 如何在 C# 中获取图表的 Excel XValue

python - 我无法获得完整的 xls 文件,只能通过 python3 请求获得表头

vba - 在 VBA 中触发完整计算

excel - 为 Shapes 制作真正的公式、条件格式和控制提示