Hi Ed,
My original idea was to build an array out of the contents
of individual table cells: ary(1) would be the contents of row 1,
and so forth. If I could search the array ahd find a match and
return the position, I would know what row the text is in.
That way, all I have to do is search the array, not iterate the
table cells every time.
Good plan! I often do it the same way.
But I think I'm stuck with iteration.
Sure, but once you have the stuff in an array, iteration is a lot faster
than iterating throught the table cells.
I'll post the code I use below.
Regards,
Klaus
Function Table2Variant(myTable As Table) As Variant
' reads uniform table into variant array.
' indices: row, column; 1-based.
' The table cells can contain paragraph marks and tabs.
' Usage: Dim vTable as Variant
' vTable=Table2Variant(myTable)
' MsgBox vTable(iRow, iCol)
Dim vTable() As Variant
Dim sText As String
sText = myTable.Range.Text
Dim i_Row As Long, i_Col As Long
Dim i_Row_Max As Long, i_Col_Max As Long
Dim i_pos_Old As Long, i_pos As Long
i_Row_Max = myTable.Rows.Count
i_Col_Max = myTable.Columns.Count
ReDim vTable(i_Row_Max, i_Col_Max)
i_pos_Old = 1
For i_Row = 1 To i_Row_Max
For i_Col = 1 To i_Col_Max
i_pos = InStr(i_pos_Old, sText, Chr(13) & Chr(7)) + 2
vTable(i_Row, i_Col) = MID(sText, i_pos_Old, i_pos - i_pos_Old - 2)
i_pos_Old = i_pos
Next i_Col
i_pos_Old = i_pos_Old + 2
Next i_Row
Table2Variant = vTable
End Function
Sub Table2Variant_TestIt()
Dim vTest() As Variant
Dim i As Long, j As Long
vTest = Table2Variant(ActiveDocument.Tables(1))
For i = 1 To UBound(vTest, 1)
For j = 1 To UBound(vTest, 2)
Debug.Print "row " & i, "col " & j, vTest(i, j)
Next j
Next i
End Sub