リスト5:サンプル(CRAPI.exe)
Option Explicit

' Open and close print engine
' ---------------------------
Declare Function PEOpenEngine Lib _
  "CRPE32.DLL" () As Integer
Declare Sub PECloseEngine Lib _
  "CRPE32.DLL" ()
Declare Function PECanCloseEngine Lib _
  "CRPE32.DLL" () As Integer
' Open and close print job (i.e. report)
' --------------------------------------
Declare Function PEOpenPrintJob Lib _
  "CRPE32.DLL" (ByVal RptName$) As Integer
Declare Function PEClosePrintJob Lib _
  "CRPE32.DLL" (ByVal PrintJob%) As Integer
' Start and cancel print job
' (i.e. print the report, usually after changing report)
' ---------------------------------------------------------------------------------
Declare Function PEStartPrintJob Lib _
  "CRPE32.DLL" (ByVal PrintJob%, ByVal WaitOrNot%) As Integer
Declare Sub PECancelPrintJob Lib _
  "CRPE32.DLL" (ByVal PrintJob%)
' Changing printer selection
' --------------------------
Declare Function PESelectPrinter Lib _
  "CRPE32.DLL" (ByVal PrintJob%, _
                ByVal PrinterDriver$, _
                ByVal PrinterName$, _
                ByVal PortName$, _
                ByVal DevMode As Long) As Integer
' Controlling print to window
' ---------------------------
Declare Function PEOutputToWindow Lib _
  "CRPE32.DLL" (ByVal PrintJob%, _
                ByVal Title$, _
                ByVal Lft%, _
                ByVal Top%, _
                ByVal Wdth%, _
                ByVal Height%, _
                ByVal style As Long, _
                ByVal PWindow As Long) As Integer
Declare Function PEPrintWindow Lib _
  "CRPE32.DLL" (ByVal PrintJob%, ByVal WaitNoWait%) As Integer
Declare Sub PECloseWindow Lib _
  "CRPE32.DLL" (ByVal PrintJob%)
Declare Function PEOutputToPrinter Lib _
  "CRPE32.DLL" (ByVal PrintJob%, ByVal nCopies%) As Integer
' A table's location is fetched and
' set using PEGetNthTableLocation and
' PESetNthTableLocation.  This name is database-dependent,
' and must be formatted correctly for the expected database.
' For example:
'     - Paradox: "c:\crw\ORDERS.DB"
'     - SQL Server: "publications.dbo.authors"
Global Const PE_TABLE_LOCATION_LEN = 256
Global Const PE_SIZEOF_TABLE_LOCATION = 258  ' # bytes in PETableLocation

Type PETableLocation
    ' initialize to # bytes in PETableLocation
    StructSize As Integer
    Location  As String * PE_TABLE_LOCATION_LEN
End Type

Declare Function PEGetNthTableLocation Lib _
  "CRPE32.DLL" (ByVal PrintJob%, _
                ByVal TableN%, _
                Location As PETableLocation) As Integer
Declare Function PESetNthTableLocation Lib _
  "CRPE32.DLL" (ByVal PrintJob%, ByVal TableN%, _
                Location As PETableLocation) As Integer
' The default behavior is for a report to use its saved data,
' rather than refresh its data from the database
' when printing a report.
Declare Function PEHasSavedData Lib _
  "CRPE32.DLL" (ByVal PrintJob%, HasSavedData%) As Integer
Declare Function PEDiscardSavedData Lib _
  "CRPE32.DLL" (ByVal PrintJob%) As Integer
Declare Function PESetFormula Lib _
  "CRPE32.DLL" (ByVal PrintJob%, ByVal FormulaName$, _
                ByVal FormulaString$) As Integer
' Print job error codes and messages
' ----------------------------------
Declare Function PEGetErrorCode Lib _
  "CRPE32.DLL" (ByVal PrintJob%) As Integer
Declare Function PEGetErrorText Lib _
  "CRPE32.DLL" (ByVal PrintJob%, TextHandle As Long, _
                TextLength%) As Integer
' Setting page margins
' --------------------
Declare Function PESetMargins Lib _
  "CRPE32.DLL" (ByVal PrintJob%, ByVal LeftMargin%, _
                ByVal RightMargin%, ByVal TopMargin%, _
                ByVal BottomMargin%) As Integer
Global Const PE_SM_DEFAULT = &H8000

' 1
  If PEOpenEngine() <> 1 Then
    intPrtErr = 1
  End If
  strRepName = IIf(gstrRootDir <> "", gstrRootDir & "\", "") & _
               mstrRptName + Chr$(0)

' 2
  intPrtJob = PEOpenPrintJob(strRepName)
  If intPrtJob = 0 Then
    intPrtErr = 2
  End If

' 3
  If intPrtErr = 0 Then
    typLocation.StructSize = PE_SIZEOF_TABLE_LOCATION
    If PEGetNthTableLocation(intPrtJob, 0, typLocation) <> 1 Then
      intPrtErr = PEGetErrorCode(intPrtJob)
    End If
  End If

' 4
  If intPrtErr = 0 Then
    typLocation.Location = mstrFileName & Chr$(0)
    If PESetNthTableLocation(intPrtJob, 0, typLocation) <> 1 Then
      intPrtErr = PEGetErrorCode(intPrtJob)
    End If
  End If

' 5
  If PEOutputToPrinter(intPrtJob, 1) <> 1 Then
    intPrtErr = PEGetErrorCode(intPrtJob)
  End If
  If intPrtErr = 0 Then

' 6
    If PEStartPrintJob(intPrtJob, True) <> 1 Then
      intPrtErr = PEGetErrorCode(intPrtJob)
    End If

' 7
    Do While PECanCloseEngine() = 0
      DoEvents
    Loop
    End If

exitList:
    On Error Resume Next

' 8
  PEClosePrintJob (intPrtJob)

' 9
  PECloseEngine