Category : Dbase (Clipper, FoxBase, etc) Languages Source Code
Archive   : TN9010.ZIP
Filename : WPWEXP.TXT

 
Output of file : WPWEXP.TXT contained in archive : TN9010.ZIP
This article is reprinted from the October 1990 edition of
TechNotes/dBASE IV. Due to the limitations of this media, certain
graphic elements such as screen shots, illustrations and some tables
have been omitted. Where possible, reference to such items has been
deleted. As a result, continuity may be compromised.

TechNotes is a monthly publication from the Ashton-Tate Software
Support Center. For subscription information, call 800-545-9364.

Exporting to WordPerfect and Microsoft Word
Larry Quaglia

One unfortunate result of the bevy of products we use today is that
transferring data from one program to the next is never as easy as it
should be. Luckily dBASE IV provides import/export commands that
support many of the major products. For those that dBASE IV doesn't
support, a small dBASE program will often do the trick.

One of the more frequent requests that we receive here at Software
Support is a means to create merge files for word processing programs
from a dBASE database. Two of the most common of these are
WordPerfect and Microsoft Word. dBASE IV does not support these file
formats directly but with a little programming it's easy to create
routines that will generate them.

Word Perfect

WordPerfect mailmerge files are referred to as Secondary Merge Files
and are nothing more than text files with some special separators.
Each field is separated by a Control-R (ASCII value 18) and a hard
carriage return (ASCII value 13). Each record is separated by a
Ctrl-E (ASCII value 5) and a hard carriage return. A typical merge
file would look like the example below.

Garnett< <-----------------Record Separator

Lena<

501 Main St.<

Reno<

NV<

89504<

(702)555-9121<

á <-----------------Field Separator

Above is a merge file for WordPerfect 4.1, 4.2 or 5.0. Version 5.1
uses delimited files created by dBASE IV directly. See the COPY TO
command in the Language Reference.

PROCEDURE WPData

You can write almost anything to a text file using the SET ALTERNATE
commands, and this procedure will use it so to write out the field and
record separators. The WPData procedure (the listing is on the
opposite page) loops through the database, writing the contents of
each field followed by the appropriate separators. Every field is
treated as a character field since WordPerfect assumes everything is
text. Each record is separated by the Control-E. The merge file is
written out with the same name as the database except it has a .txt
extension.

WPData also includes a function, DropExt(), that you may find useful.
It simply drops the extension off any file name and replaces it with
one of your choosing. The first parameter is the file name, the
second is the extension that you want to use instead.

After creating the merge file in dBASE IV, you must bring the text
file into WordPerfect using Ctrl-F5, then save it as a WordPerfect
document which then becomes your new secondary merge file.

Microsoft Word

Microsoft Word mail-merge files are even more easily created. Word
accepts merge files in two formats. One is a comma-separated,
quote-delimited text file with a first line containing the field names
separated by commas. Alternatively, you can use a comma-separated,
quote-delimited text file with a separate header file containing the
field names. For simplicity, the MSWord procedure uses the latter
format.

PROCEDURE MSWord

The header file that is created is named the same as the database with
a .hed extension. The delimited file that is created is also named
the same as the database with a .txt extension. The procedure simply
retrieves the field names and writes them to the header file, then
creates the delimited file. The header file for a MS Word mailmerge
would look like the following:

LASTNAME, FIRSTNAME, ADDRESS, CITY, STATE, ZIP, PHONE

The delimited file may contain a line like the following:

"Garnett","Lena","501 Main
St.","Reno","NV","89504","(702)555-9121"

Keep in mind that dBASE IV creates delimited files that use dates in
the format YYYYMMDD. If that format isn't suitable for your
documents, you may want to convert your date fields into character
fields before generating the merge file. This is done by modifying
the structure and changing the field type from date to character.

PROCEDURE WPData
PROCEDURE Wpdata
PARAMETERS filename
output = ""
USE (filename)
newfile =Dropext(filename,"TXT") && Get the
database filename

SET CONSOLE OFF && and
replace .DBF with .TXT.
SET ALTERNATE TO (newfile)

cntr = 1
SCAN FOR .NOT. EOF()
DO WHILE LEN(TRIM(LTRIM(FIELD(cntr))))<>0
&& Loop through each
txtstr = FIELD(cntr)
&& of the fields.
DO
CASE && Test field
types.
CASE TYPE(txtstr) = "C"
output = TRIM(&txtstr)

CASE TYPE(txtstr) = "N" .OR.
TYPE(txtstr) = "F"
output =
RTRIM(LTRIM(STR(&txtstr)))
* Write numeric fields as
characters.

CASE TYPE(txtstr) = "D"
output =
DTOC(&txtstr) && Convert dates to char.

CASE TYPE(txtstr) = "L"
output = IIF(&txtstr, "Y",
"N")
* Write logical fields as Y or
N.
CASE TYPE(txtstr) = "M"
cntr = cntr + 1
LOOP
ENDCASE

SET ALTERNATE ON
IF cntr = 1
?? TRIM(output) + CHR(18) && output
contents with Ctrl-R
ELSE
? TRIM(output) + CHR(18)
ENDIF (cntr = 1)
SET ALTERNATE OFF
cntr= cntr + 1
ENDDO

SET ALTERNATE ON
? CHR(5) &&
output End-of Record mark (Ctrl-E).
?
SET ALTERNATE OFF
cntr = 1
ENDSCAN

SET ALTERNATE TO
SET CONSOLE ON
CLEAR ALL
RETURN

FUNCTION DropExt
PARAMETERS filename,Newext
newfile = SUBSTR(filename, 1, AT(".", filename)) +
TRIM(Newext)
RETURN newfile

* EoP: WPData

PROCEDURE MSWord
PROCEDURE MSword
PARAMETERS filename

Headfile = DropExt(filename, "hed") && Grab the database
name
Newfile = DropExt(filename, "txt") && and assign new
extensions.
USE (filename)
cntr = 1
headstring = ""

DO WHILE LEN(TRIM(LTRIM(FIELD(cntr))))<>0
* This loop creates a comma-separated string of the
field names
IF TYPE(FIELD(cntr)) <> "M"
headstring = headstring + FIELD(cntr) + ", "
ENDIF
cntr = cntr + 1
ENDDO
headstring = LEFT(headstring, LEN(headstring) -2) + CHR(10)

SET CONSOLE OFF

SET ALTERNATE TO (headfile) && Write the string to the .HED
file.
SET ALTERNATE ON
?? headstring
SET ALTERNATE OFF
SET ALTERNATE TO
SET CONSOLE ON

COPY TO (newfile) TYPE DELIMITED && Write data to the
.TXT file.
CLEAR ALL
RETURN

FUNCTION DropExt
PARAMETERS filename,newext
Newfile = SUBSTR(filename, 1, AT(".", filename)) +
TRIM(newext)
RETURN newfile

* EoP: MSWord



  3 Responses to “Category : Dbase (Clipper, FoxBase, etc) Languages Source Code
Archive   : TN9010.ZIP
Filename : WPWEXP.TXT

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/