On-Line Printing

I am creating a program with an on-line printing feature.  It is a logging system which prints every new entry log directly to the printer.

My problem is how to do it since VB6 does not seem to support this feature.
And whenever I use the EndDoc command, the printer will start with a
new page.  How to go about this?

I have attached an example below:


Private Sub Form_Load()
   Timer1.Interval = 1000   ' Set Timer interval.
End Sub

Private Sub Timer1_Timer()
 Label1.Caption = Time   ' Update time display.
 Printer.Print Time
 'Printer.EndDoc
 
End Sub
rcbarotAsked:
Who is Participating?
 
adityauConnect With a Mentor Commented:
The following code should work. Call the sub PrintLine with the string which u want to print as a parameter whenever u want to print.

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 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 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 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 Type DOCINFO
    pDocName As String
    pOutputFile As String
    pDatatype As String
End Type

Private Sub PrintLine(ByVal strPrintString As String)
    Dim lngPrinter As Long
    Dim lngReturn As Long
    Dim lngDoc As Long
    Dim udtDocInfo As DOCINFO
    Dim lngWritten As Long

    lngReturn = OpenPrinter(Printer.DeviceName, lngPrinter, 0)
    If lngReturn = 0 Then
        MsgBox "The Printer is not recognized"
        Exit Sub
    End If
    udtDocInfo.pDocName = "rcbarot"
    udtDocInfo.pOutputFile = "rcbarot"
    udtDocInfo.pDatatype = vbNullString
    lngDoc = StartDocPrinter(lngPrinter, 1, udtDocInfo)
    Call StartPagePrinter(lngPrinter)
    lngReturn = WritePrinter(lngPrinter, ByVal strPrintString, Len(strPrintString), lngWritten)
    lngReturn = EndPagePrinter(lngPrinter)
    lngReturn = EndDocPrinter(lngPrinter)
    lngReturn = ClosePrinter(lngPrinter)
End Sub
0
 
cvidlerCommented:
You can't use the enddoc command (it will always start a new page), and the printer won't start printing (because vb doesn't send the data to Windows) until you do.

I don't think you can do this, unless you print directly to the printer port, using some sort of port I/O control.
0
 
rcbarotAuthor Commented:
Adjusted points to 100
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
rcbarotAuthor Commented:
I am accepting this answer but is there any other way?  I am quite new in VB Programming.  

Thanks.
0
 
rcbarotAuthor Commented:
adityau,

I have tried using the program you had given but it doesn't seem to work.  I have tried several printers, dot matrix, deskject but couldn't get it to work.  I have tested with the program below.  What am I missing?


_______________________________________
'The following code should work. Call the sub PrintLine with the string which u want to print as a parameter whenever u want to print.

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 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 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 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 Type DOCINFO
    pDocName As String
    pOutputFile As String
    pDatatype As String
End Type


Private Sub PrintLine(ByVal strPrintString As String)
    Dim lngPrinter As Long
    Dim lngReturn As Long
    Dim lngDoc As Long
    Dim udtDocInfo As DOCINFO
    Dim lngWritten As Long

    lngReturn = OpenPrinter(Printer.DeviceName, lngPrinter, 0)
    If lngReturn = 0 Then
        MsgBox "The Printer is not recognized"
        Exit Sub
    End If

    udtDocInfo.pDocName = "rcbarot"
    udtDocInfo.pOutputFile = "rcbarot"
    udtDocInfo.pDatatype = vbNullString
    lngDoc = StartDocPrinter(lngPrinter, 1, udtDocInfo)
    Call StartPagePrinter(lngPrinter)
    lngReturn = WritePrinter(lngPrinter, ByVal strPrintString, Len(strPrintString), lngWritten)
    lngReturn = EndPagePrinter(lngPrinter)
    lngReturn = EndDocPrinter(lngPrinter)
    lngReturn = ClosePrinter(lngPrinter)
End Sub


Private Sub Form_Load()
   Timer1.Interval = 1000   ' Set Timer interval.

End Sub

Private Sub Timer1_Timer()
    Label1.Caption = Time   ' Update time display.
   
    'Printer.Print Time
    PrintLine Time
   
   
End Sub

0
 
adityauCommented:
I am sorry. There is some problem. Use the following statement where u have used
udtDocInfo.pOutputFile = "rcbarot" . It should be

udtDocInfo.pOutputFile=vbNullString.

It should work.
0
 
adityauCommented:
I am sorry. There is some problem. Use the following statement where u have used
udtDocInfo.pOutputFile = "rcbarot" . It should be

udtDocInfo.pOutputFile=vbNullString.

It should work.
0
 
rcbarotAuthor Commented:
Thanks a lot pal!  I tried your corrections and worked pefectly fine.  Thanks a lot for the help.
0
 
adityauCommented:
u r most welcome boss
0
All Courses

From novice to tech pro — start learning today.