VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'The printer is an object with a library
'The problem with the Printer object Printer.print is that it spools all
'the data until
'the buffer is filled and then prints.  If you want to print realtime and do
'not want to waste paper this program prints and clears the buffer by opening
'the printer object, printing and then closing the object.
'The code in form load can be called in any subroutine.  It was put into form
'load for demonstration purposes only.

Option Explicit

Private Type DOCINFO
          pDocName As String
          pOutputFile As String
          pDatatype As String
End Type

Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
   "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
    ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias _
   "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
   pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
   pcWritten As Long) As Long

Private Sub Form_Load()

'Setting printer properties for report
    Printer.ScaleMode = vbInches
    Printer.FontSize = 12
    Printer.FontName = "Arial"

'The code below can be placed in any subroutine to be called, you do not have
'to put it in form load.
Dim sWrittenData As String

    sWrittenData = "Hi there!"
    Call PrintNow(sWrittenData)

End Sub

Private Sub PrintNow(sWrittenData As String)

Dim lhPrinter As Long
Dim lReturn As Long
Dim lpcWritten As Long
Dim lDoc As Long
Dim MyDocInfo As DOCINFO
  
  lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
  If lReturn = 0 Then
      MsgBox "The Printer Name you typed wasn't recognized."
      Exit Sub
  End If
  MyDocInfo.pDocName = "Document 1"
  MyDocInfo.pOutputFile = vbNullString
  MyDocInfo.pDatatype = vbNullString
  lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
  Call StartPagePrinter(lhPrinter)
  sWrittenData = sWrittenData & vbCrLf
  lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
     Len(sWrittenData), lpcWritten)
  lReturn = EndPagePrinter(lhPrinter)
  lReturn = EndDocPrinter(lhPrinter)
  lReturn = ClosePrinter(lhPrinter)

End Sub

