                         MAIL MERGE IN CLIPPER

by: Per Kjellqvist, HotSoft, Geneva, Switzerland  -  CS User ID 71540,2311
    Mark Bernhard, WHO, Geneva, Switzerland


While working on a recent project the idea came up to write a routine that
would merge data from one or more databases into a memoedit() text. This can
be used for normal mail merging or any instances where you would
like to provide the user with total flexibility to create his/her own ad-hoc
outputs.

The process is done in two steps;

1) Text Editing
   Using memoedit() you can create texts and save them as .txt files, or
   as memo fields in a database.  In the text there has to be some
   indication as to what is to be merged from a database. we use ^{ on
   one side and } on the other side of the field names. For example, a text
   could look like this:

   "Hi ^{name} I would like to inform you that you owe me ^{debt} dollars"

2) Merging and Printing
   To print one letter for each person in a database, simply open the database
   and start a DO WHIL loop. For each record replace all merge codes with
   the data from the database, then print the letter using mlcount() and
   memoline().

   This process sound easier than it actually is as you have to consider
   the following;
   - It is not possible to change all merge codes to a delimeter and then
     ampersand the whole text to get the real data as a macro only works
     on text strings up to 256 characters long.
   - Fields other than character will have to be transformed into character
     format.
   - To allow correct word wrapping all database text will have to be
     inserted before using mlcount() and memoline().
   - Users might put { and } where no merge is wanted.

Compile the mailm.prg and run it to see how we solved the problem.
