Mail Merge using MS Word

djharsha

New Member
Can any one give me some sample code for Mail merge using document template

its really worth if some one have it
 
Here's a basic outline of a merge automation. You've not given any real idea of the bahviour you're looking for, so this executes a merge of a template doc and a text file data source. The resulting merged doc is made visible, but you could do all sorts of things from there.

Code:
DEFINE VARIABLE chWordApp  AS COM-HANDLE     NO-UNDO.
DEFINE VARIABLE chDoc      AS COM-HANDLE     NO-UNDO.
DEFINE VARIABLE chMerge    AS COM-HANDLE     NO-UNDO.
DEFINE VARIABLE chNewDoc   AS COM-HANDLE     NO-UNDO.
 
&GLOBAL-DEFINE wdSendToNewDocument  0
 
    CREATE "Word.Application" chWordApp.
    chWordApp:ScreenUpdating = TRUE.
    chWordApp:visible        = FALSE.
    chWordApp:displayAlerts  = FALSE.
 
    /* open the template document */
    chDoc = chWordApp:documents:OPEN("c:\MergeTemplate.doc",,YES,FALSE).  
 
    /* set Mail Merge object */
    chMerge = chDoc:MailMerge.      
 
    /* open the data source */
    chMerge:OpenDataSource("c:\MergeData.txt").                   
    chMerge:SuppressBlankLines = FALSE.
 
    /* merge target is a NEW word Doc */
    chMerge:Destination = {&wdSendToNewDocument}.  
 
    /* Run merge */
    chMerge:EXECUTE.                      
 
    /* get handle to new document (the merged one) */
    chNewDoc = chWordApp:ActiveDocument.   
    chNewDoc:fields:update.
    chWordApp:visible = TRUE.
 
    RELEASE OBJECT chDoc.
    RELEASE OBJECT chMerge.    
    RELEASE OBJECT chNewDoc.    
    RELEASE OBJECT chWordApp.
 
Top