home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / WordProcessors / CBM-PS32.LZX / PageStream3 / Scripts / MailMerge.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1997-04-10  |  4.0 KB  |  142 lines

  1. /* $VER: MailMerge.rexx 1.1c (10.04.97)
  2.    Copyright 1996 SoftLogik Publishing Corporation
  3.    fixed small UI issue where variable started out as » then ended as > (1.1b 18.03.97)
  4.    May not be distributed without SoftLogik Publishing Corporation's express written permission */
  5.  
  6. /* Data Format
  7.  
  8. field1name»field2name»field3name...
  9. record1field1»record1field2»record1field3...
  10. record2field1»record2field2»record2field3...
  11. ...
  12.  
  13. where » is a tab
  14.  
  15. Data Example:
  16.  
  17. Address    City    Name    State    ZipCode
  18. 315 Consort Drive    St. Louis    Joe User    MO    63011
  19. 7178 Sonoma Way    Los Angeles    Helen Hudson    CA    95011
  20. 1234 Howe St.    Vancouver    Fred Stone    BC    V9N 5N1
  21.  
  22. */
  23.  
  24. OPTIONS RESULTS
  25. TRACE OFF
  26.  
  27. /* Make sure rexx support is opened */
  28. IF ~SHOW('L','rexxsupport.library') THEN
  29.    CALL ADDLIB('rexxsupport.library',0,-30)
  30.  
  31. ADDRESS 'PAGESTREAM'
  32.  
  33. /* OPEN THE DATA FILE */
  34. getfile title "'Select the ASCII data file'" LOAD POSBUTTON "OK" NEGBUTTON "Cancel"
  35. if rc~=0 then signal cleanup
  36. dfile=result
  37. call open(.ifile, dfile, 'R')
  38.  
  39. /* READ AND PARSE THE HEADER */
  40. dheader=readln(.ifile)
  41. fcount=1
  42. do forever
  43.     tpos=pos(d2c(9),dheader)
  44.     if tpos=0 then field.fcount=dheader
  45.         else field.fcount=left(dheader,tpos-1)
  46.     if tpos=0 then break
  47.     dheader=right(dheader,length(dheader)-tpos)
  48.     fcount=fcount+1
  49. end
  50.  
  51. /* SEE IF THE VARIABLES ALREADY EXIST */
  52. "setvariablevalue <nul>" variable field.1
  53. if rc~=0 then call createvar()
  54.  
  55. /* MAIL MERGE */
  56. /* ASK USER IF OK TO PRINT */
  57. allocarexxrequester '"Mail Merge"' 352 89
  58.     hDialog=result
  59. addarexxgadget hDialog EXIT 12 70 70 label "Print"
  60.     hDialog.ok=result
  61. addarexxgadget hDialog EXIT 270 70 70 label "Cancel"
  62.     hDialog.cancel=result
  63. addarexxgadget hDialog TEXT 12 10 336 border none string "'Enter the number of copies and then click'"
  64. addarexxgadget hDialog TEXT 12 22 336 border none string "'on Print to start printing.'"
  65. addarexxgadget hDialog STRING 64 42 70 label "C_opies" string '1'
  66.     hCopies=result
  67. doarexxrequester hDialog
  68.     action=result
  69. getarexxgadget hDialog hCopies string
  70.     copies=result
  71. freearexxrequester hDialog
  72. if action=hDialog.cancel then signal cleanup
  73.  
  74. do forever
  75.     /* READ THE RECORD */
  76.     dline=readln(.ifile)
  77.     if dline="" then break
  78.  
  79.     /* PARSE THE RECORD */
  80.     do i=1 to fcount
  81.         tpos=pos(d2c(9),dline)
  82.         if tpos=0 then line.i=dline
  83.             else line.i=left(dline,tpos-1)
  84.         if pos(d2c(39),line.i)>0 then line.i=d2c(34)||line.i||d2c(34) /* thanks Warren! */
  85.             else line.i=d2c(39)||line.i||d2c(39)
  86.         dline=right(dline,length(dline)-tpos)
  87.         if tpos=0 then break
  88.     end i
  89.  
  90.     /* UPDATE PGS3 VARIABLES */
  91.     'refresh off'
  92.     do i=1 to fcount
  93.         "setvariablevalue "line.i variable field.i
  94.     end i
  95.  
  96.     'refresh on'
  97.     'refreshwindow'
  98.     'printdocument copies 'copies' sides both scale actual output grayscale printermarks off mirror off negative off document'
  99.     if rc~=0 then break
  100. end
  101.  
  102. /* RESET VARIABLE NAMES */
  103. 'refresh off'
  104. do i=1 to fcount
  105.     "setvariablevalue «"field.i"»" variable field.i
  106. end i
  107. 'refresh on'
  108. 'refreshwindow'
  109.  
  110. call cleanup()
  111. EXIT
  112.  
  113. CREATEVAR:
  114.     do i=1 to fcount
  115.         'newvariable 'field.i' «'field.i'»'
  116.     end i
  117.     /* 'newvariable +Next «+»'
  118.         We'll use this when Find/Replace is done for a Next Record control
  119.         The script will search the text for these. If found, it will have to
  120.         replace following variable uses until the next Next control with
  121.         varname.1, etc. Then the script will have to reset multiple var groups
  122.         at once. */
  123.  
  124.     /* INFORM USER THAT VARS ARE CREATED */
  125.     allocarexxrequester '"Mail Merge"' 364 105
  126.         hDialog=result
  127.     addarexxgadget hDialog EXIT 282 88 70 label "OK"
  128.         hDialog.ok=result
  129.     addarexxgadget hDialog TEXT 8 10 356 border none string "'The mail merge variables have been created.'"
  130.     addarexxgadget hDialog TEXT 8 22 356 border none string "'Use the Type/Insert Variable » User String'"
  131.     addarexxgadget hDialog TEXT 8 34 356 border none string "'command to insert them into your document.'"
  132.     addarexxgadget hDialog TEXT 8 46 356 border none string "'When you are done, save it and then choose'"
  133.     addarexxgadget hDialog TEXT 8 58 356 border none string "'this macro again to print it.'"
  134.     doarexxrequester hDialog
  135.     freearexxrequester hDialog
  136.     signal cleanup
  137. RETURN
  138.  
  139. CLEANUP:
  140. call close(.ifile)
  141. EXIT
  142.