Read information from particular table to array

B

Beginner in vba

I have a document which have text and tables, i need to find a particular
table (can we give a name to a table?) and extract information from each
column and save it into an array variables. Each data in the columns goes
into an array variables. Can someone please guide? Thanks a lot.
 
H

Helmut Weber

Hi,
first let me say, using such short variable names,
is no good. It might be excusable, I hope,
when the subject is just to explain the principles.
And I'm a lazy typer.

Here, in a way, "t" would be the name of the table(3)
in the document's main story. Not a real name-property, though.

Then, there is no need to create different arrays,
for each column a seperate array.
Should be possible, though, but I couldn't get it to work.
An array of arrays would be interesting, sure, but
as it is useless anyway, use a 2-dimensional array.

SI assume, there are no hardly to control
complications by split and merged cells.

So off we go:

Dim t As Table ' table
Dim c As Long ' column
Dim r As Long ' row
Dim l As Long ' just a counter
Dim s As String
Set t = ActiveDocument.Tables(3) ' the table is called "t" from now on
ReDim a(t.Rows.Count, t.Columns.Count)
' and disregard index 0 of the array
' the 2-dimensional array that holds all the data
' of an ordinary, very simple table
' now put it all into the array "a"
For r = 1 To t.Rows.Count
For c = 1 To t.Columns.Count
s = t.Cell(r, c).Range.Text
s = Left(s, Len(s) - 2) ' cut off end of cell mark
a(r, c) = s
Next
Next
' testing
c = 1 ' values from column 1
For l = 1 To t.Rows.Count
Debug.Print a(l, c)
Next
c = 3 ' values from column 3
For l = 1 To t.Rows.Count
Debug.Print a(l, c)
Next

End Sub


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
M

Mats Nilsson

Hi Helmut,
I found your answer when looking for a way to handle merged cells. I´d
appreciate if you could go more into details about " there are no hardly to
control
complications by split and merged cells."

Thanks in advance and Greatings from Stockholm, Sweden.
Mats
 
H

Helmut Weber

Hi Mats,

if there are split and merged cells,
you very likely can't project a table
to an array anymore, at least not in a simple way,
expect there is e.g.
the very unlikely case that all rows contain
merged cells and split cells at the same time,
and the number of cells equals the number of cells
in the row with the most cells again.

MsgBox ActiveDocument.Tables(1).Columns.Count
returns the number of cells in the row with the most cells.

Lets say it is 8.

So, if there is a row with only two cells,
because the cells there are a result of merging,
or because the number of cells in the row with
the most cells is a result of splitting cells,
you'll get an error 5941, that the requested
member of the collection does not exist,
if you are going to use a loop like this one:

Pseudocode

for r = 1 to rowscount
for c = 1 to columnscount
msgbox cell(r,c).text
next
next

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
M

Mats Nilsson

Hi Helmut,
Thanks for answer. Your answer was the same conclusion as I´ve made when
trying to get information in an array.
I think a way out of this could be to copy the table to Excel where it seems
easier to get information about merged cells and the values in the cells.
Best Regards
Mats
 

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