Tables to arrays

D

dsc

This is kind of embarrassing, because I think I've seen it before and didn't
save it, but...

What is the best way to read string data from a table in a word document to
an array, please?

I searched the Word MVP site and the Internet and couldn't find it.

Thanks much for any assistance.
 
J

Jezebel

Dim i As Long
Dim j As Long
Dim pText As String

Dim p() As String

With Selection.Tables(1)
ReDim p(1 To .Rows.Count, 1 To .Columns.Count)

For i = 1 To .Rows.Count
For j = 1 To .Columns.Count
pText = .Cell(i, j).Range.Text
p(i, j) = Left$(pText, Len(pText) - 2)
Next
Next
End With
 
D

dsc

Thank you very much.

How did we ever live before the Internet?

More to the point, how would I live without this group?
 
J

Jezebel

You call this living?



dsc said:
Thank you very much.

How did we ever live before the Internet?

More to the point, how would I live without this group?
 
K

Klaus Linke

You call this living?

<g>

If the table is uniform (no merged cells), you can speed things up quite a
bit:

Read the whole table into a string (strTest=myTable.Range.Text).

strTest will have end-of-cell-markers = ChrW(13) & CHrW(7) at the end of
each cell, and an additional one at the end of each row.

If you don't mind working with a one-dimensional array, you can use

Dim varTable As Variant
varTable = tableTest.Range.Text
varTable = Split(varTable, ChrW(13) & ChrW(7))

If you *do* mind, you can wrap accessing this array in a function:

MsgBox CellContent(varTable, tableTest.Columns.Count, myRow:=2,
myColumn:=4)

Function CellContent(varTable As Variant, _
maxColumns As Long, _
myRow As Long, _
myColumn As Long) As String
CellContent = _
varTable(myColumn - 1 + _
(myRow - 1) * (maxColumns + 1))
End Function

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top