Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Create Array from Table in Word Document

2,622 views
Skip to first unread message

tbotsko

unread,
Feb 23, 2004, 4:35:42 PM2/23/04
to
I'm looking for a way to create a two-dimensional array
from a table in a word document. The table will always be
the same number of columns wide, but will vary in number
of rows. Any ideas how to dimension this, and then to
extract information from it in order to populate bookmarks
in the Word Document?

Peter Hewett

unread,
Feb 23, 2004, 5:04:36 PM2/23/04
to
Hi tbotsko

Are you reasonably familiar with VBA? If you are I have a class module that
will do what you want. Apart from the class module all you need it about 6
lines of code to reference it and presto instant table in an array!

If you're interested post again and I'll post the code. Once you've created
the class module you can forget about it, you will not need to modify it.

This is all the code you need to load the data in your table into an array:

Dim tdInfo As clsTableData
Dim astrData() As String

' Instantiate and initialise - use table number 2
Set tdInfo = New clsTableData
strDataSource = "F:\My Templates\Table.doc"
tdInfo.Initialise strDataSource, 2

' Load data in table into array
astrData = tdInfo.Data(1, , 1)

The currently code assumes that the document containing the table is not your
current document.

HTH + Cheers - Peter


"tbotsko" <anon...@discussions.microsoft.com> wrote in news:14a7a01c3fa54
$fcf1fca0$a301...@phx.gbl:

Jezebel

unread,
Feb 23, 2004, 5:15:10 PM2/23/04
to
Dim pTable as word.table
Dim pArray() as variant
Dim pRow as long
Dim pColumn as long

Set pTable = Selection.Tables(1) .... or however you get a
reference to the table

Redim pArray(1 to ptable.rows.count, 1 to pTable.Columns.Count)
For pRow = 1 to pTable.Rows.Count
For pColumn = 1 to pTable.Columns.Count
pArray(pRow, pColumn) = pTable.Cell(pRow, pColumn)
Next
Next


But do you need to do this? ... if you're just pulling the information out
for use elsewhere in the document, you can just as easily use the table
itself as the source array. Note that the above code won't work if the table
contains merged or split cells, or nested tables.


"tbotsko" <anon...@discussions.microsoft.com> wrote in message
news:14a7a01c3fa54$fcf1fca0$a301...@phx.gbl...

Manik

unread,
Feb 24, 2004, 12:06:06 AM2/24/04
to
first of all try to store the total no of columns and rows of a table in the variable.
dim tcol as long
dim trow as long
tcol = wrd.ActiveDocument.Tables(1).Columns.Count ' this will have total no of columns.
trow = wrd.ActiveDocument.Tables(1).Rows.Count ' this will have total no of rows.

now create a dynamic array.
Dim arr() As String 'this is this two dimensional array.

'now create a procedure which will insert the values into this array.

Sub first(ByVal row As Long, ByVal col As Long) 'function which takes rows and columns as parameter.
ReDim arr(row, col) 'dynamically increasing the size of the array.
'inserting the values into the array
For k = 1 To row
For m = 1 To col
' MsgBox (" value of k and m is :: " & k & m)
arr(k, m) = wrd.ActiveDocument.Tables(1).Cell(k, m).Range.Text
Next m
Next k

this will help u out .
from mani

tbotsko

unread,
Feb 24, 2004, 1:51:11 PM2/24/04
to
Thank you all for your help. I'll test these and see if
they work.

tbotsko

unread,
Feb 25, 2004, 4:08:36 PM2/25/04
to
Jezebel:

Thanks for the code. Simple and concise. Now another
question. I want to look for particular values in column
three of my table/array. If the value is found, I want to
put text in a bookmark using the value in column five of
the table/array in the same row. Since the data will not
always fall in the same order, the row number will change.

Also, any suggestions for the best visual basic classes
available? I'm in the San Diego area.

Thanks.

>.
>

Jezebel

unread,
Feb 25, 2004, 5:57:38 PM2/25/04
to
For pIndex = 1 to pTable.Rows.Count 'Check each row of
the table
if pTable.Cell(pIndex,3).Range = "...." then 'examine column 3
pValue = pTable.Cell(pIndex,5).Range 'Retrieve the value
.... put it into the document
end if
Next

Rather than using bookmarks, I'd suggest you use CustomDocumentProperties.
They are easier to work with. Set up the property (File > Properties >
Custom) with a dummy value, then in the body of the document use a
DOCPROPERTY field where you want the value to appear. Then in the above code
you can use

ActiveDocument.CustomDocumentProperties("MyPropName") = pValue

When your code completes you'll need to select the document and update
fields for the new value to appear.

No idea about VB classes in the San Diego area. I'm a very long way away
from your continent.

"tbotsko" <anon...@discussions.microsoft.com> wrote in message

news:181501c3fbe3$88736e00$a401...@phx.gbl...

georg...@gmail.com

unread,
Nov 12, 2018, 6:07:36 PM11/12/18
to
Hi Peter Hewett,
I know this is a very old post, but the subject is still very important.
Would you please share the class module?
I appreciate your help very much
Have a good evening
Ricardo G Lopes

Jillian A

unread,
May 20, 2021, 8:28:35 PM5/20/21
to
This is an even older post now, but I thought I'd share what I came up with because this is the thread that comes up when you google this problem

Because I load the entire table's text into a 1D array first, I only have to pull data from the doc once, this makes it a lot faster than doing cell by cell

Function TableToArray(tbl As Table, Optional include_row_1 As Boolean = True) As Variant
Dim text_arr As Variant
text_arr = Split(tbl.Range.Text, ChrW(13) & ChrW(7))

Dim tbl_arr As Variant
ReDim tbl_arr(1 To tbl.Rows.Count - IIf(include_row_1, 0, 1), 1 To tbl.Columns.Count)

Dim i As Integer, j As Integer, n As Integer
If include_row_1 Then
n = 0
Else
n = tbl.Columns.Count + 1
End If

For i = LBound(tbl_arr) To UBound(tbl_arr)
For j = LBound(tbl_arr, 2) To UBound(tbl_arr, 2) + 1
If j <= UBound(tbl_arr, 2) Then tbl_arr(i, j) = text_arr(n)
n = n + 1
Next j
Next i

TableToArray = tbl_arr
End Function
0 new messages