For everything but BSTR arrays and a Variant containing an array, I just use the following:
On BSTR arrays and a Variant containing an array, different approaches must be taken.Code:Option Explicit ' Public Declare Function ArrPtr Lib "msvbvm60" Alias "VarPtr" (a() As Any) As Long Public Declare Function GetMem2 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless. Public Declare Function GetMem4 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless. ' Public Function ArrayDims(pArray As Long) As Integer ' Won't work with BSTR arrays nor a Variant containing an array. ' Works with arrays of variants, of all other intrinsic types, of objects, and of fixed length strings. ' Example: Debug.Print ArrayDims(ArrPtr(SomeArray())) ' Returns 0 if not dimensioned. ' Dim pSA As Long If pArray = 0& Then Exit Function GetMem4 ByVal pArray, pSA ' De-reference. If pSA = 0& Then Exit Function ' Dynamic undimensioned array. GetMem2 ByVal pSA, ArrayDims End Function
-----
Here's some test code that can be thrown into a Form1:
Code:Option Explicit Private Sub Form_Load() Dim a() As Long Debug.Print "a()", ArrayDims(ArrPtr(a)) ReDim a(1, 2, 3, 4, 5) Debug.Print "a()", ArrayDims(ArrPtr(a)) End Sub




Reply With Quote
