Creating a report in MS Word using template with bookmarks

G

Guest

I would like to create a report in an MS Word based on a Word template with
bookmarks. One bookmark will contain data from several records.

If the data were just a single table, I could 1) create an ADO recordset, 2)
use the recordset’s GetString method to get data from the recordset, 3) copy
it to the Word document using the InsertAfter method of the Word Range object
of the bookmark’s range, 4) then convert the tab and carriage-return
delimited string to a table and format.

The problem I am having is the data does not come from a single table.
After each row are “subrows†from a different table containing information
from a child table. One possible approach is to create a large recordset
containing the “parent†and “child†rows together, then with this one
recordset follow the method above to get the recordset data into the Word
document.

To me, this seems very cumbersome, and the formatting options would apply to
the whole table, unless I figured out a way to format the “parent†and
“child†rows in the table differently.

The other approach is to build the entire report in Access using a subreport
within the report, then use the OutputTo method to create an .rtf. The big
disadvantage is it takes away the option of the users being able to modify a
Word template and I will need to persist in Access other textual information
for the report that the user can modify.

I would appreciate any other ideas.

Thanks,

John
 
J

John Nurick

Hi John,

One way is to include a bookmarked "template" table in the Word
template. This is air code:

Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim rsMain As ADODB.Recordset
Dim R As Long, C As Long

...

Set oTable = oDoc.Bookmarks("XXX").Range.Tables(1)
R = 2 'assume first row is column headings
With oTable.Rows(R)
Do Until rsMain.EOF
For C = 1 to .Cells.Count
.Cells(C).Range.Text = rsMain.Fields(C-1).Value
'Format cell as required
...
Next C
If R = oTable.Rows.Count Then
oTable.Rows.Add
'Format new row as required
...
End If
R = R + 1
rsMain.MoveNext
Loop
End With

You could either open one recordset that includes all the fields for all
the rows, and only insert the appropriate fields in the document for
each kind of row; or you could open two recordsets and use them
alternately.
 
G

Guest

John,

Thanks very much for the detailed explanation and code. I'll give it a try.

John
 

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