Microsoft KB Archive/172853: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "[[../foxpro/q" to "[[../")
m (Text replacement - "&" to "&")
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
{|
{|
|width="100%"|
|width="100%"|
= HOWTO: Create an ASCII Text Output File with Headers & Footers =
= HOWTO: Create an ASCII Text Output File with Headers & Footers =


'''ID: Q172853'''
'''ID: Q172853'''
Line 23: Line 23:
== SUMMARY ==
== SUMMARY ==


Attempting to route a report to an ASCII text file from FoxPro 2.x for MS- DOS using the command REPORT FORM &lt;report name&gt; TO FILE &lt;file name&gt;, creates an ASCII text file without printer control codes. However, attempting to route a report to an ASCII text file from FoxPro for Macintosh using the command REPORT FORM &lt;report name&gt; TO FILE &lt;file name&gt;, creates an ASCII text file with embedded printer control codes.<br />
Attempting to route a report to an ASCII text file from FoxPro 2.x for MS- DOS using the command REPORT FORM <report name> TO FILE <file name>, creates an ASCII text file without printer control codes. However, attempting to route a report to an ASCII text file from FoxPro for Macintosh using the command REPORT FORM <report name> TO FILE <file name>, creates an ASCII text file with embedded printer control codes.<br />
<br />
<br />
In FoxPro 2.x for Windows, the simplest way to create an ASCII text file from a report is to use the Generic/Text only printer driver within Windows. However, if for some reason you cannot install this printer driver, you can use the following sample code as a workaround. The sample code below creates a report programmatically and sends it to an ASCII text file.
In FoxPro 2.x for Windows, the simplest way to create an ASCII text file from a report is to use the Generic/Text only printer driver within Windows. However, if for some reason you cannot install this printer driver, you can use the following sample code as a workaround. The sample code below creates a report programmatically and sends it to an ASCII text file.
Line 36: Line 36:


<pre class="CODESAMP">  *begin program
<pre class="CODESAMP">  *begin program
   m.usefile=sys(2004)              &amp;&amp; get the start up directory
   m.usefile=sys(2004)              && get the start up directory
   * add a path to the clients file
   * add a path to the clients file
   m.usefile=m.usefile+&quot;sample\organize\dbfs\clients.dbf&quot;
   m.usefile=m.usefile+"sample\organize\dbfs\clients.dbf"
   USE (m.usefile)                  &amp;&amp; use the clients table
   USE (m.usefile)                  && use the clients table
   gnhandle=FCREATE('output.txt',0) &amp;&amp; create an output file
   gnhandle=FCREATE('output.txt',0) && create an output file
   * set up some variables to track pagination
   * set up some variables to track pagination
   gnpage=1                        &amp;&amp; page count variable
   gnpage=1                        && page count variable
   gnlines=0                        &amp;&amp; line count variable
   gnlines=0                        && line count variable
   GO TOP                          &amp;&amp; go to the first record
   GO TOP                          && go to the first record
   * Now loop  through the table
   * Now loop  through the table
   DO WHILE !EOF()
   DO WHILE !EOF()
       * If the number of lines is 0, put in a page header
       * If the number of lines is 0, put in a page header
       IF gnlines&gt;=65
       IF gnlines>=65
         gnlines=0                  &amp;&amp; reset the line count to 0
         gnlines=0                  && reset the line count to 0
         gnpage=gnpage+1            &amp;&amp; increment the page count
         gnpage=gnpage+1            && increment the page count
       ENDIF
       ENDIF
       DO CASE
       DO CASE
       CASE gnlines=0
       CASE gnlines=0
         =pagehead()                &amp;&amp; procedure to write page header
         =pagehead()                && procedure to write page header
         LOOP
         LOOP
       CASE gnlines=56
       CASE gnlines=56
         =pagefoot()                &amp;&amp; procedure to write page footer
         =pagefoot()                && procedure to write page footer
         LOOP
         LOOP
       OTHERWISE
       OTHERWISE
         gnlines=gnlines+1          &amp;&amp; increment the line count
         gnlines=gnlines+1          && increment the line count
         * concatenate four character fields
         * concatenate four character fields
         out_line=client_id+SPACE(3)+company+SPACE(3)+city+SPACE(3)+state
         out_line=client_id+SPACE(3)+company+SPACE(3)+city+SPACE(3)+state
         =FPUTS(gnhandle,out_line)  &amp;&amp; output the string to the ASCII file
         =FPUTS(gnhandle,out_line)  && output the string to the ASCII file
         * FPUTS() automatically adds a carriage return &amp; line feed
         * FPUTS() automatically adds a carriage return & line feed
         * change fputs to fwrite if you want to control insertion of
         * change fputs to fwrite if you want to control insertion of
         * the carriage return &amp; line feed characters
         * the carriage return & line feed characters
         SKIP                      &amp;&amp; skip to the next record
         SKIP                      && skip to the next record
       ENDCASE
       ENDCASE
   ENDDO
   ENDDO
   * If not at the end of the page, pad blank lines
   * If not at the end of the page, pad blank lines
   IF gnlines&lt;56
   IF gnlines<56
       out_line=&quot;&quot;
       out_line=""
       FOR i=gnlines TO 56
       FOR i=gnlines TO 56
         =FPUTS(gnhandle,out_line)
         =FPUTS(gnhandle,out_line)
         gnlines=gnlines+1
         gnlines=gnlines+1
       NEXT
       NEXT
       =pagefoot()                  &amp;&amp; write the page footer
       =pagefoot()                  && write the page footer
   ENDIF
   ENDIF
   =FCLOSE(gnhandle)                &amp;&amp; close the open output file
   =FCLOSE(gnhandle)                && close the open output file
   RETURN
   RETURN


   PROCEDURE pagehead
   PROCEDURE pagehead
   =spacer()                        &amp;&amp; insert blank lines
   =spacer()                        && insert blank lines
   * Place  the title into the file
   * Place  the title into the file
   out_line=&quot;This is Sample ASCII Text Output&quot;
   out_line="This is Sample ASCII Text Output"
   gnlines=gnlines+1                &amp;&amp; increment the line count
   gnlines=gnlines+1                && increment the line count
   =FPUTS(gnhandle,out_line)        &amp;&amp; write the page header
   =FPUTS(gnhandle,out_line)        && write the page header
   =spacer()                        &amp;&amp; insert blank lines
   =spacer()                        && insert blank lines
   RETURN
   RETURN


   PROCEDURE pagefoot
   PROCEDURE pagefoot
   =spacer()                        &amp;&amp; insert blank lines
   =spacer()                        && insert blank lines
   * Place the date and page number in the footer of the file
   * Place the date and page number in the footer of the file
   out_line=DTOC(DATE())+SPACE(50)+&quot;Page &quot;+LTRIM(STR(gnpage))
   out_line=DTOC(DATE())+SPACE(50)+"Page "+LTRIM(STR(gnpage))
   =FPUTS(gnhandle,out_line)        &amp;&amp; write the page footer
   =FPUTS(gnhandle,out_line)        && write the page footer
   =spacer()                        &amp;&amp; insert blank lines
   =spacer()                        && insert blank lines
   gnlines=0                        &amp;&amp; reset the line count to 0
   gnlines=0                        && reset the line count to 0
   gnpage=gnpage+1                  &amp;&amp; increment the page count
   gnpage=gnpage+1                  && increment the page count
   RETURN
   RETURN


   PROCEDURE spacer
   PROCEDURE spacer
   * Padding four lines in this loop
   * Padding four lines in this loop
   out_line=&quot;&quot;
   out_line=""
   FOR i=1 TO 4
   FOR i=1 TO 4
       gnlines=gnlines+1
       gnlines=gnlines+1

Latest revision as of 12:29, 21 July 2020

HOWTO: Create an ASCII Text Output File with Headers & Footers

ID: Q172853



The information in this article applies to:

  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, 2.5b, 2.6, 2.6a
  • Microsoft FoxPro for Macintosh, versions 2.5b, 2.5c, 2.6a




SUMMARY

Attempting to route a report to an ASCII text file from FoxPro 2.x for MS- DOS using the command REPORT FORM <report name> TO FILE <file name>, creates an ASCII text file without printer control codes. However, attempting to route a report to an ASCII text file from FoxPro for Macintosh using the command REPORT FORM <report name> TO FILE <file name>, creates an ASCII text file with embedded printer control codes.

In FoxPro 2.x for Windows, the simplest way to create an ASCII text file from a report is to use the Generic/Text only printer driver within Windows. However, if for some reason you cannot install this printer driver, you can use the following sample code as a workaround. The sample code below creates a report programmatically and sends it to an ASCII text file.


MORE INFORMATION

If printer control codes are present in an ASCII text file, they can cause significant difficulties when you try to share the file with another application or user. The sample code below illustrates hot to use low-level file input and output (I/O) functions to create an ASCII text output file with headers and footers.

Sample Code

   *begin program
   m.usefile=sys(2004)              && get the start up directory
   * add a path to the clients file
   m.usefile=m.usefile+"sample\organize\dbfs\clients.dbf"
   USE (m.usefile)                  && use the clients table
   gnhandle=FCREATE('output.txt',0) && create an output file
   * set up some variables to track pagination
   gnpage=1                         && page count variable
   gnlines=0                        && line count variable
   GO TOP                           && go to the first record
   * Now loop  through the table
   DO WHILE !EOF()
      * If the number of lines is 0, put in a page header
      IF gnlines>=65
         gnlines=0                  && reset the line count to 0
         gnpage=gnpage+1            && increment the page count
      ENDIF
      DO CASE
      CASE gnlines=0
         =pagehead()                && procedure to write page header
         LOOP
      CASE gnlines=56
         =pagefoot()                && procedure to write page footer
         LOOP
      OTHERWISE
         gnlines=gnlines+1          && increment the line count
         * concatenate four character fields
         out_line=client_id+SPACE(3)+company+SPACE(3)+city+SPACE(3)+state
         =FPUTS(gnhandle,out_line)  && output the string to the ASCII file
         * FPUTS() automatically adds a carriage return & line feed
         * change fputs to fwrite if you want to control insertion of
         * the carriage return & line feed characters
         SKIP                       && skip to the next record
      ENDCASE
   ENDDO
   * If not at the end of the page, pad blank lines
   IF gnlines<56
      out_line=""
      FOR i=gnlines TO 56
         =FPUTS(gnhandle,out_line)
         gnlines=gnlines+1
      NEXT
      =pagefoot()                   && write the page footer
   ENDIF
   =FCLOSE(gnhandle)                && close the open output file
   RETURN

   PROCEDURE pagehead
   =spacer()                        && insert blank lines
   * Place  the title into the file
   out_line="This is Sample ASCII Text Output"
   gnlines=gnlines+1                && increment the line count
   =FPUTS(gnhandle,out_line)        && write the page header
   =spacer()                        && insert blank lines
   RETURN

   PROCEDURE pagefoot
   =spacer()                        && insert blank lines
   * Place the date and page number in the footer of the file
   out_line=DTOC(DATE())+SPACE(50)+"Page "+LTRIM(STR(gnpage))
   =FPUTS(gnhandle,out_line)        && write the page footer
   =spacer()                        && insert blank lines
   gnlines=0                        && reset the line count to 0
   gnpage=gnpage+1                  && increment the page count
   RETURN

   PROCEDURE spacer
   * Padding four lines in this loop
   out_line=""
   FOR i=1 TO 4
      gnlines=gnlines+1
      =FPUTS(gnhandle,out_line)
   NEXT
   RETURN
   *end of program file 


REFERENCES

For additional information on creating ASCII output files, please see the following articles in the Microsoft Knowledge Base:

Q139144 PRB: Can't Print a Plain, Unformatted Report to a File

Q121631 HOWTO: Print a Report to a File in FoxPro for Windows

Additional query words:

Keywords          : kbcode kbVFp FoxWin FxprgGeneral FxprintGeneral 
Version           : MACINTOSH:2.5b,2.5c,2.6a; WINDOWS:2.5,2.5a,2.5b,2.6,2.6a
Platform          : MACINTOSH WINDOWS 
Issue type        : kbhowto 

Last Reviewed: August 12, 1999
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.