How to Pass an Array from C# (VSTO) project to VBA Macro

亡梦爱人 提交于 2019-12-06 13:52:52
Vincent De Smet

Okay, I've should have read this better: Variants should be avoided, so if I have the choice of writing my VBA, I better do it without a variant but with a proper Array.

Secondly, I was using VBA arrays wrong, I should do the following for multi-dimensional arrays (source):

My VBA code now looks like this:

Sub fillInteriorMulti(rg As Range, Arr() As Long)
    Dim N As Long, Ndx1 As Long, Ndx2 As Long
    Dim icol As Long
    Dim irow As Long
    Dim NumDims As Long


    // Custom Function: Get the number of array dimensions.
    // NumberOfArrayDimensions will return 0
    // if the array is not allocated.

    NumDims = NumberOfArrayDimensions(Arr:=Arr)
    Select Case NumDims
        Case 0
            // unallocated array
            Exit Sub
        Case 1
            // single dimensional array
            For N = LBound(Arr) To UBound(Arr)
                With rg(N, 1).Interior
                    .ColorIndex = Arr(N)
                    .PatternColorIndex = xlAutomatic
                    .PatternColor = xlSolid
                End With
            Next N
        Case 2
            // 2 dimensional array
            For Ndx1 = LBound(Arr, 1) To UBound(Arr, 1)
                For Ndx2 = LBound(Arr, 2) To UBound(Arr, 2)
                    With rg(Ndx1, Ndx2).Interior
                        .ColorIndex = Arr(Ndx1, Ndx2)
                        .PatternColorIndex = xlAutomatic
                        .PatternColor = xlSolid
                    End With
                Next Ndx2
            Next Ndx1
        Case Else
            // Too many dimensions - Do Nothing
    End Select
End Sub

Public Function NumberOfArrayDimensions(Arr As Variant) As Integer
    // NumberOfArrayDimensions
    // This function returns the number of dimensions of an array. An unallocated dynamic array
    // has 0 dimensions. This condition can also be tested with IsArrayEmpty.

    Dim Ndx As Integer
    Dim Res As Integer
    On Error Resume Next
    // Loop, increasing the dimension index Ndx, until an error occurs.
    // An error will occur when Ndx exceeds the number of dimension
    // in the array. Return Ndx - 1.
    Do
        Ndx = Ndx + 1
        Res = UBound(Arr, Ndx)
    Loop Until Err.Number <> 0

    NumberOfArrayDimensions = Ndx - 1
End Function

Finally the C# Code to test this:

            int[] test3 = new int[3];
            test3[0] = 15;
            test3[1] = 15;
            test3[2] = 48;

            int[,] test4 = new int[2, 2];
            test4[0, 0] = 15;
            test4[0, 1] = 15;
            test4[1, 0] = 15;
            test4[1, 1] = 15;

            this.Application.Run("Sheet1.fillInteriorMulti", rg, test4,
                missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing);

Ugly hack or proof of concept, convert double array to string. Pass on String.

Sub fillInteriorString(rg As Range, str As String)
    Dim i As Long, j As Long
    i = 1
    j = 1
    a = Split(str, "@")
    For Each part In a
        b = Split(part, ",")
        For Each colorIdx In b
            With rg(i, j).Interior
                .ColorIndex = colorIdx
                .PatternColorIndex = xlAutomatic
                .PatternColor = xlSolid
            End With
            j = j + 1
        Next
        j = 1
        i = i + 1
    Next
End Sub

C# Code:

            string testString = "15,15,48@48,48,15";


            this.Application.Run("Sheet1.fillInteriorString",  rg,  testString,
                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

anyway, I know it works - now I'm looking into SafeArrays in the hope I can still pass the array instead of a string.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!