HOWTO: Using SET PRINTER TO NAME to Specify Report Destination

Last reviewed: February 10, 1997
Article ID: Q162798
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 3.0b, 5.0

SUMMARY

In order to specify that a Visual FoxPro report be sent to a particular printer, it is customary to either change the Windows default printer or use the PROMPT clause on the REPORT command to allow the user to select the printer. However, it is possible to change the desired destination printer without using either of these techniques. This article explains some methods for doing this using SET PRINTER TO NAME [printer name].

MORE INFORMATION

The SET PRINTER TO NAME command will allow the currently selected printer within Visual FoxPro to be set to any printer installed under Windows. The list of installed printers can be read into an array with the APRINTERS() function, or a single printer name can be returned by the GETPRINTER() dialog.

NOTE: For this procedure to work, the report form must be saved with the Default Printer selected from the Name dropdown box within the Print Setup dialog. This dialog is called from the Page Setup dialog found under the File menu.

Example 1

The following program will display the GETPRINTER() dialog to allow selection of a printer from the installed printers. If OK is selected in the GetPrinter dialog, a report called Test will be sent to the selected printer. If the Cancel button is selected in the GetPrinter dialog, the report will not be printed.

   * Beginning of program for Example 1
   * *******************************************************
   * This program assumes a report called Test which has been
   * saved with the default printer in the Print Setup dialog.
   *
   LOCAL lcPrinter

   lcPrinter = ''
   lcPrinter = GETPRINTER()

   IF !EMPTY(lcPrinter)
      SET PRINTER TO NAME (lcPrinter)
      REPORT FORM Test TO PRINTER NOCONSOLE
   ELSE
      WAIT WINDOW 'Print Job Cancelled' TIME 1
   ENDIF
   *
   * End of program for Example 1

Notes:

Under Visual FoxPro 3.0 and 3.0b, the GETPRINTER() dialog is different, and the dialog will allow changing portrait/landscape settings among others. These will be ignored when the report is printed.

Example 2

The following program will display a custom form which has a dropdown list populated with an array created with APRINTERS(). If you choose the "Print Report" command button, a report called Test will be sent to the selected printer. If you choose the Cancel button, the report will not be printed.

   * Beginning of program for Example 2
   *********************************************************
   * This program assumes a report called Test which has been
   * saved with the default printer in the Print Setup dialog.
   *
   PUBLIC oform
   oform=CREATEOBJECT("form1")
   oform.SHOW

   DEFINE CLASS form1 AS FORM
      AutoCenter = .T.
      Height = 158
      Width = 327
      Caption = "Printer Dialog"
      Name = "Form1"
      DIMENSION aprintarray[1]

      ADD OBJECT combo1 AS COMBOBOX WITH ;
          ROWSOURCETYPE = 5, ;
          ROWSOURCE = "thisform.aPrintArray", ;
          HEIGHT = 25, ;
          LEFT = 24, ;
          STYLE = 2, ;
          TOP = 48, ;
          WIDTH = 276, ;
          NAME = "Combo1"

      ADD OBJECT command1 AS COMMANDBUTTON WITH ;
          TOP = 108, ;
          LEFT = 48, ;
          HEIGHT = 27, ;
          WIDTH = 84, ;
          CAPTION = "Print Report", ;
          DEFAULT = .T., ;
          NAME = "Command1"

      ADD OBJECT command2 AS COMMANDBUTTON WITH ;
          TOP = 108, ;
          LEFT = 180, ;
          HEIGHT = 25, ;
          WIDTH = 84, ;
          CANCEL = .T., ;
          CAPTION = "Cancel", ;
          NAME = "Command2"

      PROCEDURE combo1.INIT
          LOCAL lnI
          FOR lnI = 1 TO APRINTERS(THISFORM.aprintarray)
              *   Note below that you are adding a leading space. This
              *   prevents a network printer in Windows NT from appearing
              *   disabled in the combo due the leading "\"
              THISFORM.aprintarray[lnI,1] = SPACE(1) + ;
                 THISFORM.aprintarray[lnI,1]
          ENDFOR
          * Set initial value of combo
          THIS.Requery()
          IF '5.0' $ VERSION()
              * This sets the combo initial value of the dropdown to the
              * default printer - This will not work in 3.0/3.0b since
              * SET('PRINTER', 2) is not available
              FOR EACH a_element IN THISFORM.aprintarray
                  IF UPPER(SET('PRINTER',2))$UPPER(a_element)
                      THIS.Value = a_element
                  ENDIF
              ENDFOR
          ELSE
              * If in 3.0/3.0b, set to first element in list.
              THIS.Value = THISFORM.aprintarray[1]
          ENDIF
      ENDPROC

      PROCEDURE command1.CLICK
          SET PRINTER TO NAME (ALLTRIM(THISFORM.combo1.VALUE))
          REPORT FORM test TO PRINTER NOCONSOLE
          RELEASE THISFORM
      ENDPROC

      PROCEDURE command2.CLICK
          RELEASE THISFORM
      ENDPROC

   ENDDEFINE
   *
   * End of program for Example 2

REFERENCES

Visual FoxPro 5.0 Help file


KBCategory: kbprint kbhowto
KBSubcategory: FxtoolReportdes FxprintDriver vfoxwin
Additional reference words: 3.00 3.00b 5.00 How To



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.