Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing to a file 1

Status
Not open for further replies.

AdaHacker

Programmer
Sep 6, 2001
392
US
I'm modifying an old FoxPro for DOS program. Currently, it will do some processing and print out a nice little report. In an effort to move toward a paperless office (or, at least kill less that half a forest each month), I've been asked to make the report print to a file which will be stored on the network.

The question is, how? I tried a
set device to file report.txt
statement, but it doesn't appear to have worked correctly. I end up with a file that has a few records, none of the headings, and a whole lot of blank lines. I also noticed that a lot of stuff that looked like the report headers was being printed to the screen. What's going on?

The program currently prints using the following type of code:
Code:
set device to printer 
set print on
@ a,b SAY whatever
It also uses
? whatever
statements to print the headers. This is done in a separate program file.

Does anyone have any ideas? Is there anything I should look for?
 
Use

SET DEVICE TO PRINT
and
SET PRINT TO FILE whatever.TXT [ADDITIVE] is optional

When it's done, use
SET PRINT TO
and
SET DEVICE TO SCREEN

Dave S.
 
Dave,

Thanks for the response. I tried your suggestion and, as a result, I'm making progress. I'm now getting all the records in the report. On the down side, I'm still getting the report headers printing to the screen.

The body of my report is generated by a file called snowbody.prg (it's for tracking county snow removal costs). This part is working now. The report headers are generated by snowhead.prg. In snowbody.prg I have now set

set device to print
set print to file report.txt

I tried setting something similar in snowhead.prg, but it just messed things up. How can I get the headings to also be written to the file?

Also, just so I'm sure, when I'm done I do
set print to screen
set device to screen
right? I just wanted to be sure that's what you meant.
 
Ada,
Before your the command:

SET DEVICE TO PRINT

Issue the command

SET CONSOLE OFF

then, instead of set device to screen,
issue:

SET CONSOLE ON

Thanks,
-Scott

Please let me know if this has helped! s-)
 
Here ya go:

SET CONSOLE OFF
SET PRINTER TO FILE report.txt
SET DEVICE TO PRINTER
SET PRINTER ON

?'? asdfasdfasdfasdf'
?'? asdfasdfasdfasdf'
?'? asdfasdfasdfasdf'
?'? asdfasdfasdfasdf'

@ 1, 1 SAY '@ asdfasdfasdfasdf'
@ 2, 1 SAY '@ asdfasdfasdfasdf'
@ 3, 1 SAY '@ asdfasdfasdfasdf'
@ 4, 1 SAY '@ asdfasdfasdfasdf'

SET PRINTER OFF
SET PRINTER TO
SET CONSOLE ON
SET DEVICE TO SCREEN

This produces the following output in report.txt:

? asdfasdfasdfasdf
? asdfasdfasdfasdf
? asdfasdfasdfasdf
? asdfasdfasdfasdf FormFeed

@ asdfasdfasdfasdf
@ asdfasdfasdfasdf
@ asdfasdfasdfasdf
@ asdfasdfasdfasdf

Note I have put a formfeed in because since the print row is incremented for each '?', so '@ 1, 1' will be prior to the current row thereby causing an eject.

Dave S.
 
Excellent! I've got all the output now! Thanks a lot!

One last question, Dave. At the end of the report, I put
SET PRINTER TO
as in your post. On that line, it gives me the following error:
This device does not exist on the network. reading device
Abort, Retry, Fail?
How can I fix this? Is there some keyword, e.g. EMPTY, NULL, or NOTHING, that I can set it to rather than just blank space?
 
Glad you got it.

SET PRINTER TO with nothing after it sets it back to the default printer, but maybe FPD don't quite know how to interpret what it is at that point. Not sure. You might try just using SET PRINTER TO lpt1: or some other generic port, or even to the actual printer name associated with it just to release the text file as the print job.

Dave S.
 
Ack. Sorry. One more question. Thanks for the help, though. You have no idea how much I appreciate it.

I seem to have messed something up. I need to be able to print to BOTH a file and the printer. The file part is working fine now. The printer part is not. (At least I think it's not, since I don't have a local printer to test it with and it doesn't recognize the network printer.) I tried to use the same program file to do both using if statements. Here's the code:
Code:
set color to w/b,gr+/n,b
set talk off
use snowtemp
store 12 to linecnt
set fixed on
set escape on

if usefile = 0
    set device to printer
    set print on
else
* NEW CODE BEGINS
    set console off
    set printer to file report.txt
    set device to printer
    set printer on
* NEW CODE ENDS
endif

* Do the report building


if usefile = 0
    set device to screen
    @ 0,0 say chr(27)+"E"
    set print off
else
* NEW CODE BEGINS
    set printer off
    set printer to
    set console on
    set device to screen
* NEW CODE ENDS
endif
I once again seem to get the report headers being dumped to the screen, but this time when I try to print. By all rights, I should be getting a printer not ready error (I did before). I checked the header printing program, and it's unchanged from the original code. I tried exiting FoxPro to reset the environment (I thought maybe something was still set to output to the console).
What's wrong?
 
Well, you're out of luck. @/SAYS and ?/?? can only go to one output device at a time, and the "printer" can only be one device at a time, not a printer and a file, just as you cannot print to more than one physical printer at a time. You are going to have to run the code twice, or go about another way such as file i/o along with printer or screen i/o.

Dave S.
 
But I am running the code twice. Maybe I wasn't clear on that. Look at the code snippet I posted. I'm using the usefile variable to determine whether to print to a file or printer. In the program that calls it, I set usefile to 0, so that it will print to the printer, and then, when I finish the report, I set usefile to 1 and run the same program again. Basically, what (I think) I'm doing is setting the i/o to the printer, running the report, setting the i/o to a file, and running the report agian. The code I posted before was the only real change to the actual report generating. The correct part of the if startments are executed each time, but, for some reason, the i/o that should go to the printer seems to be ending up (at least in part) on the screen. I hope that makes my problem a little clearer. Thanks!
 
I didn't realize you were running the code twice. Try it this way:

SET COLOR TO w/b,gr+/N,b
SET TALK OFF
USE snowtemp
STORE 12 TO linecnt
SET FIXED ON
SET ESCAPE ON

SET CONSOLE OFF
IF usefile = 0
SET PRINTER TO whatever &&... either network printer name or lpt1: etc.,
ELSE
SET PRINTER TO FILE REPORT.txt
ENDIF
SET DEVICE TO PRINTER
SET PRINTER ON

* Do the report building

SET PRINTER OFF
SET PRINTER TO lpt1
SET CONSOLE ON
SET DEVICE TO SCREEN


Dave S.
 
you might try after you print to file print the file
ergo
!type report.txt>prn

only works if you left your printer formatting in the report
(i use this to preview )
!edit report.txt
alt-f p
 
Thanks a lot for all your help guy! It's slow going, but this forum is really helping me get through this project!

I figured out the last question, and now I feel kind of dumb. I thought the code I had would work, and, in fact, it did work. I had a user test it on her machine this morning, and it printed out perfectly. After I deleted the FOXUSER.* files from my working directory, I started getting the "no printer" error messages I had before. Apparently it was just some printer setting that got saved when I didn't want it to. I'll have to do some reading on that.

Anyway, thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top