0

I'm facing difficulty when creating some kind of a report in vfp. I created a text file, used the ?/?? command to write to the file, then opened it using web browser (OLE Object in vfp form).

Set Printer To 'C:\temp\test.txt'
set printer on
set console off

?? 'test'

and then i called my prg :

PARAMETERS pUrl

lcURL = pUrl
PUBLIC oForm
oForm = CREATEOBJECT('IEForm')
oForm.SHOW()
oForm.oIE.Navigate2(m.lcURL)
READ events

DEFINE CLASS IEForm AS FORM

   CAPTION = 'Report Preview'   
   HEIGHT = 512
   WIDTH = 792
   AUTOCENTER = .t.
   ADD OBJECT oIE AS OLECONTROL WITH ;
      HEIGHT=512,WIDTH=792,OLECLASS="Shell.Explorer",ANCHOR=195

   PROCEDURE oIE.REFRESH
   NODEFAULT
   ENDPROC

ENDDEFINE 

and used the C:\temp\test.txt as the url.

This works fine. But the problem occurs when I use fcreate to create the text file( not using the already existing text file)

 lcfile ='C:\temp\'+SYS(2015)+'.txt'
 lchandle=FCREATE(lcfile)
 Set Printer To lcfile

sometimes the problem occurs when using ? ( resulting in blank doc), and sometimes the browser can't navigate to the file.

The second problem is : the browser can't navigate to the text file if i set the windowtype property to modal in the IEform. I need to set it modal because I have to delay the rest of the execution until the preview form is closed.

I hope I described the situation clear enough. Thank You for helping Me :)

wong chung yie
  • 175
  • 1
  • 4
  • 14
  • i've managed to make a work around for the modal form issue, by moving all my coding ( all the part that come after calling the IEform) to the unload method of the IEForm. So, only one problem remains : How to create a text file that can be used in commands such as 'set printer to' and than browse it using web browser in a vfp form. thanks. – wong chung yie May 21 '12 at 11:48

1 Answers1

1

I would avoid using SET PRINTER TO, and just use the fcreate(), fwrite() and fclose(), especially if writing a large amount of text. If only writing a small amount of data, I would just build one long string by just appending each component to the prior and add cr/lf instead of "?" for new lines, then write the entire result to a file ONCE via StrToFile(). This way, you don't have to worry if things are actually closed (if you dont clear the set printer off, set printer to, or fclose(). Being an incomplete file, the browser won't navigate since the file handle may be "locked" to read anything.

Option 1 -- building a string

lcCRLF = chr(13)+chr(10)
lcMyOutput = "Some Text to Start with" + lcCRLF ;
           + "Today is: " + dtoc( date() ) + lcCRLF

use SomeTable
scan
    lcMyOutput = lcMyOutput + "Some Field: " + SomeCharField;
       + "A numeric field: " + transform( SomeNumberField, "999,999.99" ) + lcCRLF
endscan

lcMyOutput = lcMyOutput + "All Done"

lcOutputFile = "C:\Temp\" + sys(2015) + ".txt"
StrToFile( lcMyOutput, lcOutputFile )

Option 2 -- using fcreate(), fwrite(), fclose()

lnHandle = 0
do while .t.
   lcOutputFile = "C:\Temp\" + sys(2015) + ".txt"
   lnHandle = fcreate( lcOutputFile )
   */ Only get out if we have a proper handle and thus
   */ we are exclusive to it for duration of our process
   if( lnHandle > 0 )
      exit
   endif
enddo

lcCRLF = chr(13)+chr(10)
fwrite( lnHandle, "Some Text to Start with" + lcCRLF ;
               + "Today is: " + dtoc( date() ) + lcCRLF )

use SomeTable
scan
    fwrite( lnHandle, "Some Field: " + SomeCharField;
           + "A numeric field: " + transform( SomeNumberField, "999,999.99" ) + lcCRLF )
endscan

fwrite( lnHandle, "All Done" )

fclose( lnHandle ) 

For running your "batch" file attempt with the RUN command, build the WHAT you want into a string, then run that... ex:

lcCommandToRun = '"' + sys(5) + sys(2003) + '\Compress.tzs"'
run ( lcCommandToRun )

I don't know if it was failing due to a space being in the path of your sys(2003) result turning the RUN into a multi-parameter interpretation, it would fail. By wrapping the entire text within double-quotes, it will prevent such "path" issue.

ADDITIONAL FEEDBACK

Trying to simulate a preview of a DOT-matrix printer using "?" to a text file is not something I would try. I would just do a standard report and draw it with all the data you are doing manually with the "?". If the user want's to preview the report, or send to a laser printer, let the normal report version handled through Windows for generation, font, and such do it for you. Leave the ancient code going to dot-matrix with whatever specific font rendering code as you have. I would just keep the actual report version CLOSE TO your "?" version.

DRapp
  • 47,638
  • 12
  • 72
  • 142
  • Thank You, DRapp.I have experienced using strtofile before. I used it to create a script file and then use it like: RUN /N SYS(5)+SYS(2003)+'\compress.tzs'. but it failed. the script file did formed and when i doubleclicked the script later it did worked. I wonder if there is a procedure that has to be done after creating a file before we can actually do something with the file? Many thanks. – wong chung yie May 22 '12 at 03:05
  • On the other hand, I wasn't sure that the run command failed because of strtofile itself. Please advice me.. – wong chung yie May 22 '12 at 03:23
  • @wongchungyie, check revision on "RUN" implementation. – DRapp May 22 '12 at 03:26
  • You are right, my experience with RUN has nothing to do with the strtofile. I tested it using execscript and it worked fine. Back to the original question: I was creating a report that will be printed in a dot matrix printer, but has to be previewed first. So i use a procedure to create the report format, both for the preview and to the printer. If I use the strtofile, i won't be able to choose the font and the size. If i use the fwrite approach, I think I will get difficulties to print to a usb / laser printers ( I prefer not to use netuse method ). Please correct me if I'm wrong. Thank You. – wong chung yie May 28 '12 at 05:08
  • @wongchungyie, see latest revision – DRapp May 28 '12 at 12:24
  • Thank You for the reply. I think I will apply just like you said in the revision. Thank You, God Bless. – wong chung yie May 29 '12 at 00:43