VBA Mailmerge

M

Mike

I am pretty new to mail merges. I am trying to write a macro to
extract a field value from a merge. The only way I see doing this is
creating a mail merge macro. If I write a mail merge macro how do I
display the contents of the field values on the document? Also if I
want to record all of the values for a certain field say FieldA, how
do I extract the value to an array? In other words which merge field
can I use to set to a array variable that will give me the field
value? I know that I will probably have to somehow loop through the
field value to store each value for FieldA in the array.

Thanks

Mike
 
P

Peter Jamieson

In essence, when you execute a mailmerge from a macro, you lose control
until Word finishes merging, so you don't get to see the individual
values coming in from the data source. However, if you are working with
Word 2002/2003, you can use Word's mailmerge events to get (say)

Activedocument.MailMerge.DataSource.DataFields("the field name").Value

(or some such) as Word processes each record. You would have to
initialise and populate the array yourself.

You can also inspect these values in "normal" VBA (i.e. outside event
handlers) by moving the activerecord one at a time and inspecting the
records.

If you know what records the merge is supposed to be processing (e.g. if
you do not allow user filtering, sorting, or data source selection),
you could consider using a more familiar way to get the data. For
example, for several types of data including Access, Excel and SQL
Server data, you could connect using ADO. You might need to ensure that
the Mailmerge Data Source was closed before doing so.

Peter Jamieson
 

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