arraylist.ToArray convert to excel.range() problem
From: Enrique Bustamante (ebustamante_at_xstratacoal.com)
Date: 05/06/04
- Next message: Scott Bellware: "P/Invoke Newbie Question"
- Previous message: andrea catto': "__fastcall functions in a static library... I can't call them from managed functions, why ?"
- Messages sorted by: [ date ] [ thread ]
Date: 5 May 2004 17:01:50 -0700
I am creating a function that finds ranges and returns an array with
all ranges found.
I have a problem I have not been able to solve.
The function is below. See the note at the bottom of the procedure.
There is something I am missing here. Please help.
Thank you very much.
Public Shared Function FindAll(ByVal targetRange As Excel.Range, _
ByVal What As Object, _
Optional ByVal LookIn As XlFindLookIn = _
XlFindLookIn.xlFormulas, _
Optional ByVal LookAt As XlLookAt = XlLookAt.xlWhole, _
Optional ByVal SearchOrder As XlSearchOrder = _
XlSearchOrder.xlByColumns, _
Optional ByVal MatchCase As Boolean = False) As Excel.Range()
'validate range
If Not targetRange Is Nothing Then
Dim cell As Excel.Range
With targetRange
'create an arraylist to hold the ranges. initial size
'could be smaller
Dim cellsFound As ArrayList = New ArrayList _
(targetRange.Cells.Count)
Dim somethingFound As boolean
'find first cells
cell = .Find(What, LookIn:=LookIn, LookAt:=LookAt, _
SearchOrder:=SearchOrder, _
MatchCase:=MatchCase)
'if there is at least one cell found look for more
If Not cell Is Nothing Then
somethingFound = true
'address to stop when the find method wraps
'up to first cell
Dim firstAddressFound As String = cell.Address
Do
'add the cell to the arraylist
cellsFound.Add(cell)
'find next cell
cell = .FindNext(cell)
'if we are back to the first cell, exit loop
Loop While cell.Address <> firstAddressFound
'if cells were found, return the answer,
'otherwise return nothing
If somethingFound Then
'resize the array so there are not empty items
cellsFound.TrimToSize()
'***here my problem. I get the same exception for all 3
'instructions I have tried so far.
'exception:
'A first chance exception of
'type 'System.InvalidCastException'
'occurred in OfficeUtilities.exe
'Additional information: Specified cast is not valid.
'note that: all elements added to the arraylist are
'of the same type: excel.range
'the arraylist is trimmed so there are not empty nor
'undefined items
Return CType(cellsFound.ToArray(), _
Excel.Range())
Return DirectCast(cellsFound.ToArray(), _
Excel.Range())
Return DirectCast(cellsFound.ToArray(GetType _
(Excel.Range)), Excel.Range())
End If
End If
End With
End If
End Function
- Next message: Scott Bellware: "P/Invoke Newbie Question"
- Previous message: andrea catto': "__fastcall functions in a static library... I can't call them from managed functions, why ?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|