MailMerge and Word tables

M

Mystery Man

This posting relates to using MailMerge fields to populate a Word
table.

I am using MergeFields as placeholders (like bookmarks) for tabular
data as well as single fields. I can get single fields populated very
easily by simply iterating through the Merge fields and replacing them
with the appropraite data.

Populating the table gets more complicated. I setup a Word table that
has 2 rows and a fixed number of columns. Row one contains the table
header as it appears to the user. In each column of the second row, I
place a merge field that ultimately relates to a column in a dataset.
This works to a fashion when populating the table, but the way I have
done this is a bit messy and not bullet proof. I would really like to
do either rewrite this or do some extra checking to ensure that a
merge field that is only relevant for a table is within a table and
the same in reverse.

The specific questions that I have are as follows (in relation to the
above):

Can I somehow get all the tables in a document programatically?
Given a table (as fetched above), can I get all the merge fields used
within this table? (ie the ones that are on row two of the table
template)
Failing that, given a merge field, can I see what Word table that is
on?
What is the easiest way to populate the table? cell by cell (which I
am doing) or all in one hit. If it is the latter, which method do I
use?

I hope all of this is clear.
 
C

Cindy M -WordMVP-

Hi Mystery,
Can I somehow get all the tables in a document programatically?
You can iterate through the Tables collection:

For Each tbl in ActiveDocument.tables
Given a table (as fetched above), can I get all the merge fields used
within this table? (ie the ones that are on row two of the table
template)
Continuing from above:
For each fld in tbl.Range.Fields
If fld.Type = wdFieldMergefield Then
Failing that, given a merge field, can I see what Word table that is
on?
Yes... with reservations. What, more specifically, would identify the
table for you? Place it in a bookmark? "look at" the column headers?
What is the easiest way to populate the table? cell by cell (which I
am doing) or all in one hit. If it is the latter, which method do I
use?
Well, cell-by-cell is certainly the slowest conceivable way. My
preference is to get the data into a delimited string, assign it to the
Range.Text property, then convert the range to a table. This wouldn't
mesh too well with the existing table, however. I'd say you'd need to
delete that and put the string in its place...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
M

Mystery Man

Thanks Cindy

With you help, I have it working. However, as you mentioned cell by
cell is too slow. In addition, calling the method to insert a row in
the table multiple times is even slower. Therefore, I am investigating
your suggestion of putting the data into a Range.Text and then
converting it to a table. Getting the new table is easy enough.
However, we need to be able to preserve properties of the cells such
as width, alignment, font name, font size, colour, etc that are in the
original document. Can I programattically get all of these attributes
from a table and then apply them to all cells in a particular column
(except the first row) once the new table has been created?
 

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