Create Array from Table in Word Document

T

tbotsko

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?
 
P

Peter Hewett

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
 
J

Jezebel

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.
 
M

Manik

first of all try to store the total no of columns and rows of a table in the variable
dim tcol as lon
dim trow as lon
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 arra
For k = 1 To ro
For m = 1 To co
' MsgBox (" value of k and m is :: " & k & m
arr(k, m) = wrd.ActiveDocument.Tables(1).Cell(k, m).Range.Text
Next
Next

this will help u out
from mani
 
T

tbotsko

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.
 
J

Jezebel

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.
 

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