CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2001
    Location
    Bangalore
    Posts
    7

    Spooling Problems while using WritePrinter() functions on to Dot Matrix Printers

    We are using the following code for sending printing instructions to the Dot Matrix Printers, but we are not able to spool on to the printer on converting to executable.
    Running .VBP through VB we able spool on to the printer.
    Please suggest, what would be reason on why the executable is unable to spool.

    ******** VB CODE **********

    Option Explicit

    Private Type DOCINFO
    cbSize As Long
    lpszDocName As String
    lpszOutput 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, 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 StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As Byte) 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 Declare Function GetLastError Lib "kernel32" () As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)

    Private Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hDC As Long, lpdi As DOCINFO) As Long
    Private Declare Function EndDoc Lib "gdi32" (ByVal hDC As Long) As Long

    Public cnnPrintTest As New ADODB.Connection 'Connection Object

    Private Sub Command1_Click()
    Dim lhprinter As Long
    Dim lReturn As Long
    Dim lpcWritten As Long
    Dim strData As String
    Dim lDoc As Long
    Dim MyDocInfo As DOCINFO
    Dim lngI As Long

    Dim recPrintTest As New ADODB.Recordset
    Dim recGetDtl As New ADODB.Recordset

    Dim strPrintName As String

    lReturn = OpenPrinter("\\sys-70\EPSON FX-80+", lhprinter, 0)

    If lReturn = 0 Then
    MsgBox "Error, while opening printer", vbCritical
    End
    End If

    MyDocInfo.cbSize = Len(MyDocInfo)
    MyDocInfo.lpszDocName = "BILL Print"
    MyDocInfo.lpszOutput = vbNullString

    lDoc = StartDocPrinter(lhprinter, 1, 0)
    If lDoc <= 0 Then
    MsgBox "Cannot Spool to the file", vbCritical
    End If
    Call StartPagePrinter(lhprinter)

    strData = "This is to be printed" & vbNewLine
    'strData = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & "-"
    lReturn = WritePrinter(lhprinter, ByVal strData, Len(strData), 1)

    strData = "This is to be printed" & vbNewLine
    'strData = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & "-"
    lReturn = WritePrinter(lhprinter, ByVal strData, Len(strData), 1)
    lReturn = EndDocPrinter(lhprinter)
    lReturn = ClosePrinter(lhprinter)

    End Sub





  2. #2
    Join Date
    Oct 2001
    Posts
    3

    Re: Spooling Problems while using WritePrinter() functions on to Dot Matrix Printers

    If the printer is directly connected to your computer this problems occur!!!

    The problem is not in WritePrinter it is in StartDocPrinter.

    This doesnot occur while the printer is in Network!!!

    Try

    Open "LPT1" for Output As #1
    instead of Spooling.



    Muraleedharan K.S.
    Project Leader
    Apollo Tyres Ltd.

  3. #3
    Join Date
    Oct 2001
    Posts
    3

    Re: Spooling Problems while using WritePrinter() functions on to Dot Matrix Printers

    [Private Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
    Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
    Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
    Private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As Byte) As Long
    Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long

    Private Sub Print_File(FName As String)
    Dim StrToPrint As String
    Dim LngPrinted As Long
    Dim lhPrinter As Long
    Dim PrntErr As Long

    If (FName = "") Then
    MsgBox "Nothing to Print", vbCritical, "Error"
    Exit Sub
    End If

    ' Me.Tag = FName

    Shell "Edit.com " + Me.Tag, vbMaximizedFocus

    Call setDefaultPrinter

    PrntErr = OpenPrinter(Printer.DeviceName, lhPrinter, 0&)
    If (PrntErr = 0) Then
    MsgBox "Error in Printing ", vbCritical, "Error"
    Exit Sub
    End If

    PrntErr = StartDocPrinter(lhPrinter, 1, 0&)
    If (PrntErr = 0) Then
    'MsgBox "Error in Spooling ", vbCritical, "Error"
    'Printer is Directly connected to the Machine
    Call Prnt_2_Port(FName)
    Exit Sub
    End If

    Open FName For Input As #1
    StrToPrint = ""
    While Not EOF(1)
    Line Input #1, StrToPrint
    StrToPrint = StrToPrint + Chr(13) + Chr(10)
    WritePrinter lhPrinter, ByVal StrToPrint, Len(StrToPrint), LngPrinted
    Wend

    EndDocPrinter lhPrinter
    ClosePrinter lhPrinter
    Close #1
    End Sub

    Private Sub Prnt_2_Port(FName As String)
    Open FName For Input As #1
    Open "LPT1" For Output As #2
    StrToPrint = ""
    While Not EOF(1)
    Line Input #1, StrToPrint
    StrToPrint = StrToPrint
    Print #2, StrToPrint
    Wend
    Close #2
    Close #1
    End Sub

    Private Sub CmdPrint_Click()
    Call Print_File(Me.Tag)
    End Sub

    Private Sub CmdView_Click()
    Shell "Edit.com " + Me.Tag, vbMaximizedFocus
    End Sub
    ]

    Muraleedharan K.S.
    Project Leader
    Apollo Tyres Ltd.

  4. #4
    Join Date
    Oct 2001
    Location
    Central Floeida
    Posts
    5

    Re: Spooling Problems while using WritePrinter() functions on to Dot Matrix Printers

    Fixed Problem in your code.


    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, pDefault as Any) as Long
    private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (byval hPrinter as Long, byval Level as Long, pDocInfo as DOC_INFO_1) 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 Declare Function GetLastError Lib "kernel32" () as Long
    private Type DOC_INFO_1
    pDocName as string
    pOutputFile as string
    pDatatype as string
    End Type

    private Sub Command1_Click()
    Dim lhprinter as Long
    Dim lReturn as Long
    Dim lpcWritten as Long
    Dim strData as string
    Dim lDoc as Long
    Dim di as DOC_INFO_1
    Dim lngI as Long
    Dim delay as Long
    Dim strPrintName as string
    Dim strJunkToPrint as string

    'Junk to print
    strJunkToPrint = "Gannet Technologies Inc. Your partner in TECH." & vbCrLf
    strJunkToPrint = strJunkToPrint & "1-800-555-12345 more junk"


    'set up the printer
    'Network
    lReturn = OpenPrinter("\\ServerName\PrinterName", lhprinter, byval 0&)
    'Local or network mapped printer
    'lReturn = OpenPrinter("HP LaserJet", lhprinter, byval 0&)
    'lReturn = OpenPrinter("Local Printer Name", lhprinter, byval 0&)


    If lReturn = 0 then
    MsgBox "error, while opening printer", vbCritical
    End
    End If
    lReturn = 0

    di.pOutputFile = vbNullString
    di.pDatatype = "RAW"
    di.pDocName = ""

    lDoc = StartDocPrinter(lhprinter, 1, di)

    If lDoc <= 0 then
    MsgBox "Cannot Spool to the file" & lDoc, vbCritical
    End If
    Call StartPagePrinter(lhprinter)

    strData = "This is to be printed ONE" & vbNewLine
    lReturn = WritePrinter(lhprinter, byval strData, len(strData), 1)
    If lReturn = 0 then
    MsgBox "error, Did not write Line ONE", vbCritical
    End
    End If
    lReturn = 0


    strData = "This is to be printed TWO" & vbNewLine
    lReturn = WritePrinter(lhprinter, byval strData, len(strData), 1)
    If lReturn = 0 then
    MsgBox "error, Did not write TWO", vbCritical
    End
    End If
    lReturn = 0


    lReturn = WritePrinter(lhprinter, byval strJunkToPrint, len(strJunkToPrint), 1)
    If lReturn = 0 then
    MsgBox "error, Did not write Junk!" & vbCrLf & "Now they don't have my Phone Number", vbCritical
    End
    End If
    lReturn = 0



    strData = Chr(12) 'Form Feed
    lReturn = WritePrinter(lhprinter, byval strData, len(strData), 1)
    If lReturn = 0 then
    MsgBox "error, WritePrinter Form Feed", vbCritical
    End
    End If
    lReturn = 0

    lReturn = EndDocPrinter(lhprinter)
    If lReturn = 0 then
    MsgBox "error, EndDocPrinter", vbCritical
    End
    End If
    lReturn = 0

    lReturn = ClosePrinter(lhprinter)
    If lReturn = 0 then
    MsgBox "error, ClosePrinter", vbCritical
    End
    End If
    lReturn = 0

    End Sub












Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured