Calling tables from Word documents into a larger Word document.

B

blasphemouskiwi

Hello,

We are compiling a large report that will include hundreds of tables
of data. The data are all in an Access database. We want to export
data from Access in the form of individual Word tables, and then
insert those tables into the large report automatically via VB script,
using bookmarks as placeholders until then. Can this be done?

Thanks,
Matt
 
D

Doug Robbins - Word MVP

The following code will create a table in a Word document and populate it
with the data from a table in an Access database.

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim dtable As Table, drow As Row
'Open a database
Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)
'Add a table to the document with one row and as many fields as there are in
the database table
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
'Populate the cells in the Word table with the data from the current
record
For i = 1 To myActiveRecord.Fields.Count
drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
Next i
'Add a new row to the Word table and access the next record
Set drow = dtable.Rows.Add
myActiveRecord.MoveNext
Loop
'The last row will be empty, so delete it
drow.Delete
'Then close the database
myActiveRecord.Close
myDataBase.Close


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

Manfred F

Hi Matt,

Hello,

We are compiling a large report that will include hundreds of tables
of data. The data are all in an Access database.

when You deal with large tables in Word or with a large number of tables in
a single document, You'll run into performance issues (experienced in W2002,
W2003). In this case, to add a table, try to insert the table content as CSV
into a bookmark and then convert it to text and apply formatting.

Regards,
Manfred
 

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