/* $VER: PlaceGraphicAt.rexx 1.0 (04.02.95)
   Copyright 1995 Soft-Logik Publishing Corporation
   May not be distributed without Soft-Logik Publishing Corporation's express written permission */

OPTIONS RESULTS
TRACE OFF

/* Make sure rexx support is opened */
IF ~SHOW('L','rexxsupport.library') THEN
   CALL ADDLIB('rexxsupport.library',0,-30)

ADDRESS 'PAGESTREAM'

/* OPEN THE DATA FILE */
getfile title "'Select the ASCII data file'" LOAD POSBUTTON "Ok" NEGBUTTON "Cancel"
if rc~=0 then signal cleanup
dfile=result
call open(.ifile, dfile, 'R')

/* READ AND PARSE THE HEADER */
dheader=readln(.ifile)
fcount=1
do forever
    tpos=pos(d2c(9),dheader)
    if tpos=0 then field.fcount=dheader
        else field.fcount=left(dheader,tpos-1)
    if tpos=0 then break
    dheader=right(dheader,length(dheader)-tpos)
    fcount=fcount+1
end

/* SEE IF THE VARIABLES ALREADY EXIST */
"setvariablevalue <nul>" variable field.1
if rc~=0 then call createvar()

/* MAIL MERGE */
/* ASK USER IF OK TO PRINT */
allocarexxrequester '"Mail Merge"' 364 63
    handle.req=result
addarexxgadget handle.req EXIT 12 46 70 label "_Print"
    handle.req.ok=result
addarexxgadget handle.req EXIT 282 46 70 label "_Cancel"
    handle.req.cancel=result
addarexxgadget handle.req TEXT 8 10 356 border none string "'Is everything ready for printing? Click on'"
addarexxgadget handle.req TEXT 8 22 356 border none string "'Print to start printing, or Cancel to exit.'"
doarexxrequester handle.req
    action=result
freearexxrequester handle.req
if action=handle.req.cancel then signal cleanup

do forever
    /* READ THE RECORD */
    dline=readln(.ifile)
    if dline="" then break

    /* PARSE THE RECORD */
    do i=1 to fcount
        tpos=pos(d2c(9),dline)
        if tpos=0 then line.i=dline
            else line.i=left(dline,tpos-1)
        line.i=d2c(39)||line.i||d2c(39)
        dline=right(dline,length(dline)-tpos)
        if tpos=0 then break
    end i

    /* UPDATE PGS3 VARIABLES */
    do i=1 to fcount
        "setvariablevalue "line.i variable field.i
    end i

    'refreshwindow'
/*  'printdocument copies 1 page 1 sides both scale actual output grayscale printermarks off mirror off negative off'*/
end

/* RESET VARIABLE NAMES */
do i=1 to fcount
    "setvariablevalue <"field.i">" variable field.i
end i
'refreshwindow'

call cleanup()
EXIT

CREATEVAR:
    do i=1 to fcount
        'newvariable 'field.i' «'field.i'»'
    end i

    /* INFORM USER THAT VARS ARE CREATED */
    allocarexxrequester '"Mail Merge"' 364 105
        handle.req=result
    addarexxgadget handle.req EXIT 147 88 70 label "_Ok"
        handle.req.ok=result
    addarexxgadget handle.req TEXT 8 10 356 border none string "'The mail merge variables have been created.'"
    addarexxgadget handle.req TEXT 8 22 356 border none string "'Use the Type/Insert Variable » User String'"
    addarexxgadget handle.req TEXT 8 34 356 border none string "'command to insert them into your document.'"
    addarexxgadget handle.req TEXT 8 46 356 border none string "'When you are done, save it and then choose'"
    addarexxgadget handle.req TEXT 8 58 356 border none string "'this macro again to print it.'"
    doarexxrequester handle.req
    freearexxrequester handle.req
    signal cleanup
RETURN

CLEANUP:
call close(.ifile)
EXIT
