home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / dbase / tn9010.arj / WPWEXP.TXT < prev   
Text File  |  1991-03-05  |  9KB  |  230 lines

  1. This article is reprinted from the October 1990 edition of
  2. TechNotes/dBASE IV.  Due to the limitations of this media, certain
  3. graphic elements such as screen shots, illustrations and some tables
  4. have been omitted.  Where possible, reference to such items has been
  5. deleted.  As a result, continuity may be compromised.  
  6.  
  7. TechNotes is a monthly publication from the Ashton-Tate Software
  8. Support Center.  For subscription information, call 800-545-9364.
  9.  
  10. Exporting to WordPerfect and Microsoft Word
  11. Larry Quaglia
  12.  
  13. One unfortunate result of the bevy of products we use today is that
  14. transferring data from one program to the next is never as easy as it
  15. should be.  Luckily dBASE IV provides import/export commands that
  16. support many of the major products.  For those that dBASE IV doesn't
  17. support, a small dBASE program will often do the trick.  
  18.  
  19. One of the more frequent requests that we receive here at Software
  20. Support is a means to create merge files for word processing programs
  21. from a dBASE database.  Two of the most common of these are
  22. WordPerfect and Microsoft Word.  dBASE IV does not support these file
  23. formats directly but with a little programming it's easy to create
  24. routines that will generate them.  
  25.  
  26. Word Perfect
  27.  
  28. WordPerfect mailmerge files are referred to as Secondary Merge Files
  29. and are nothing more than text files with some special separators. 
  30. Each field is separated by a  Control-R (ASCII value 18) and a hard
  31. carriage return (ASCII value 13).  Each record is separated by a
  32. Ctrl-E (ASCII value 5) and a hard carriage return.  A typical merge
  33. file would look like the example below.
  34.  
  35. Garnett<                <-----------------Record Separator
  36.  
  37. Lena<
  38.  
  39. 501 Main St.<
  40.  
  41. Reno<
  42.  
  43. NV<
  44.  
  45. 89504<
  46.  
  47. (702)555-9121<
  48.  
  49. ß               <-----------------Field Separator
  50.  
  51. Above is a merge file for WordPerfect 4.1, 4.2 or 5.0.  Version 5.1
  52. uses delimited files created by dBASE IV directly.  See the COPY TO
  53. command in the Language Reference.
  54.  
  55. PROCEDURE WPData
  56.  
  57. You can write almost anything to a text file using the SET ALTERNATE
  58. commands, and this procedure will use it so to write out the field and
  59. record separators.  The WPData procedure (the listing is on the
  60. opposite page) loops through the database, writing the contents of
  61. each field followed by the appropriate separators.  Every field is
  62. treated as a character field since WordPerfect assumes everything is
  63. text.  Each record is separated by the Control-E.  The merge file is
  64. written out with the same name as the database except it has a .txt
  65. extension.  
  66.  
  67. WPData also includes a function, DropExt(), that you may find useful. 
  68. It simply drops the extension off any file name and replaces it with
  69. one of your choosing.  The first parameter is the file name, the
  70. second is the extension that you want to use instead. 
  71.  
  72. After creating the merge file in dBASE IV, you must bring the text
  73. file into WordPerfect using Ctrl-F5,  then save it as a WordPerfect
  74. document which then becomes your new secondary merge file.
  75.  
  76. Microsoft Word
  77.  
  78. Microsoft Word mail-merge files are even more easily created.  Word
  79. accepts merge files in two formats. One is a comma-separated,
  80. quote-delimited text file with a first line containing the field names
  81. separated by commas.  Alternatively, you can use a comma-separated,
  82. quote-delimited text file with a separate header file containing the
  83. field names.  For simplicity, the MSWord procedure uses the latter
  84. format.  
  85.  
  86. PROCEDURE MSWord
  87.  
  88. The header file that is created is named the same as the database with
  89. a .hed extension.  The delimited file that is created is also named
  90. the same as the database with a .txt extension.  The procedure simply
  91. retrieves the field names and writes them to the header file, then
  92. creates the delimited file.  The header file for a MS Word mailmerge
  93. would look like the following:
  94.  
  95.                 LASTNAME, FIRSTNAME, ADDRESS, CITY, STATE, ZIP, PHONE
  96.  
  97. The delimited file may contain a line like the following:
  98.  
  99.                 "Garnett","Lena","501 Main
  100. St.","Reno","NV","89504","(702)555-9121"
  101.  
  102. Keep in mind that dBASE IV creates delimited files that use dates in
  103. the format YYYYMMDD.  If that format isn't suitable for your
  104. documents, you may want to convert your date fields into character
  105. fields before generating the merge file.  This is done by modifying
  106. the structure and changing the field type from date to character. 
  107.  
  108. PROCEDURE WPData
  109. PROCEDURE Wpdata
  110.                 PARAMETERS filename
  111.                 output = ""
  112.                 USE (filename)
  113.                 newfile =Dropext(filename,"TXT")                && Get the
  114. database filename
  115.  
  116.                 SET CONSOLE OFF                                         && and
  117. replace .DBF with .TXT.
  118.                 SET ALTERNATE TO (newfile)
  119.  
  120.                 cntr = 1
  121.                 SCAN FOR .NOT. EOF()
  122.                         DO WHILE LEN(TRIM(LTRIM(FIELD(cntr))))<>0       
  123. && Loop through each 
  124.                                 txtstr = FIELD(cntr)                    
  125. && of the fields. 
  126.                                 DO
  127. CASE                                                             && Test field
  128. types.
  129.                                         CASE TYPE(txtstr) = "C"
  130.                                                 output = TRIM(&txtstr)
  131.  
  132.                                         CASE TYPE(txtstr) = "N" .OR.
  133. TYPE(txtstr) = "F"
  134.                                                 output =
  135. RTRIM(LTRIM(STR(&txtstr)))              
  136.                                                 * Write numeric fields as
  137. characters.
  138.  
  139.                                         CASE TYPE(txtstr) = "D" 
  140.                                                 output =
  141. DTOC(&txtstr)                    && Convert dates to char.
  142.  
  143.                                         CASE TYPE(txtstr) = "L"
  144.                                                 output = IIF(&txtstr, "Y",
  145. "N")
  146.                                                 * Write logical fields as Y or
  147. N.
  148.                                         CASE TYPE(txtstr) = "M"
  149.                                                 cntr = cntr + 1
  150.                                                 LOOP
  151.                                 ENDCASE
  152.  
  153.                                 SET ALTERNATE ON
  154.                                 IF cntr = 1
  155.                                         ?? TRIM(output) + CHR(18) && output
  156. contents with Ctrl-R
  157.                                 ELSE
  158.                                         ? TRIM(output) + CHR(18)
  159.                                 ENDIF (cntr = 1)
  160.                                 SET ALTERNATE OFF
  161.                                 cntr= cntr + 1
  162.                         ENDDO
  163.  
  164.                         SET ALTERNATE ON
  165.                         ? CHR(5)                                        &&
  166. output End-of Record  mark (Ctrl-E).
  167.                         ?
  168.                         SET ALTERNATE OFF
  169.                         cntr = 1
  170.                 ENDSCAN
  171.  
  172.                 SET ALTERNATE TO
  173.                 SET CONSOLE ON
  174.                 CLEAR ALL
  175. RETURN
  176.  
  177. FUNCTION DropExt
  178.                 PARAMETERS filename,Newext
  179.                 newfile = SUBSTR(filename, 1, AT(".", filename)) +
  180. TRIM(Newext)
  181. RETURN newfile
  182.  
  183. * EoP: WPData
  184.  
  185. PROCEDURE MSWord
  186. PROCEDURE MSword
  187.                 PARAMETERS filename
  188.  
  189.                 Headfile = DropExt(filename, "hed")     && Grab the database
  190. name 
  191.                 Newfile = DropExt(filename, "txt")      && and assign new
  192. extensions.
  193.                 USE (filename)
  194.                 cntr = 1
  195.                 headstring = ""
  196.  
  197.                 DO WHILE LEN(TRIM(LTRIM(FIELD(cntr))))<>0       
  198.                         * This loop creates a comma-separated string of the
  199. field names
  200.                         IF TYPE(FIELD(cntr)) <> "M"
  201.                                 headstring = headstring + FIELD(cntr) + ", " 
  202.                         ENDIF   
  203.                         cntr = cntr + 1
  204.                 ENDDO
  205.                 headstring = LEFT(headstring, LEN(headstring) -2) + CHR(10)
  206.  
  207.                 SET CONSOLE OFF
  208.  
  209.                 SET ALTERNATE TO (headfile)   && Write the string to the .HED
  210. file.
  211.                 SET ALTERNATE ON
  212.                 ?? headstring
  213.                 SET ALTERNATE OFF
  214.                 SET ALTERNATE TO
  215.                 SET CONSOLE ON
  216.  
  217.                 COPY TO (newfile) TYPE DELIMITED        && Write data to the
  218. .TXT file.
  219.                 CLEAR ALL
  220. RETURN
  221.  
  222. FUNCTION DropExt
  223.                 PARAMETERS filename,newext
  224.                 Newfile = SUBSTR(filename, 1, AT(".", filename)) +
  225. TRIM(newext)
  226. RETURN newfile
  227.  
  228. * EoP: MSWord
  229.  
  230.