2013-07-09 1 views
4

vb.net COM interop을 사용하여 Microsoft Excel에서 작업하고 vb에서 vb.net로 배열을 전달하는 데 문제가 있습니다. VB에서 설정할 필요가있는 VB.net 코드에 PointPairs 속성이 있고 2 차원 배열을 전달하는 데 문제가 있습니다. 나는 두 개의 1D 배열을 Sub에 전달하여 vb.net에서 속성을 설정하는 것뿐만 아니라 2D 배열로 속성을 명시 적으로 설정하려고 시도했지만 아무 것도 시도하지 않은 것 같습니다.VBA에서 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 Basic 또는 Sub or Function not defined이라는 오류 메시지가 나타납니다. 나는 또한 <MarshalAs()>을 사용하여 시도했지만 이전에는 사용 해본 적이 없으며 vb와 vb.net간에 배열을 전달하는 데 사용하는 방법에 대한 많은 문서를 찾을 수 없습니다. 어떤 제안이나 솔루션이 솔루션에 관심있는 사람들을위한

답변

2

에 미리

덕분에, 나는 내가 필요 정확히이었다이 기사를 발견했다.

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

나는 VBA에서 복식 두 1D 배열로 2 차원 배열을 깨고 객체로 vb.net로를 전달하고 문서에 설명 된대로 수정할했다. 다음과 같이 SetPointPairs Sub를 변경하고 .net 코드의 Object에서 Array로 변환하는 Private 함수를 추가했습니다.

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