Home All Groups Group Topic Archive Search About
Author
27 Nov 2007 3:36 AM
CWYap
Hi guys,

Below is my vb6 code to print file through network shared printer:

    Dim iLPT as integer, iFilePtr as integer, sPrintPath as string, sFile as
string
    Dim iLoop as integer, lFileSize as long, Maxsize as long, sBuffer as
string

    iLPT = FreeFile
    sPrintPath = "\\inetsrvr\matrix"       'this is my network printer path
    sFile = "C:\Test.txt"          'this is a testing text file
    lFileSize = len(sFile)
    MaxSize = 8192

    Open sPrintPath For Binary As iLPT

    iFilePtr = FreeFile
    Open sFile For Binary As iFilePtr
    iLoop = lFileSize \ MaxSize
    sBuffer = Space$(MaxSize)
    For i = 1 To iLoop
        Get #iFilePtr, , sBuffer
        Put #iLPT, , sBuffer
    Next
    sBuffer = Space$(lFileSize Mod MaxSize)
    Get #iFilePtr, , sBuffer
    Put #iLPT, , sBuffer

    Close iFilePtr
    Close iLPT

Here is my problem.
This code had run on Windows XP for few years without error, but it can't
run on Vista. Does open port printing not supported in Vista anymore?

Anyone with similiar problem and solution? Thank you.

regards,
CW Yap

Author
27 Nov 2007 11:51 AM
Mike Williams
"CWYap" <CW***@discussions.microsoft.com> wrote in message
news:D8C6B0D3-5C0F-4ADC-AF91-F11D18BEE643@microsoft.com...

> Below is my vb6 code to print file through network shared printer:
> [big snip]
> Open sPrintPath For Binary As iLPT

Actually that method (opening printer port or printer device name as a file)
does not work in Vista even on my locally attached printer (at least on my
own two Vista machines) and it certainly doesn't work over my "laptop to
main" simple home network. I'm not so sure that it even worked on many XP
systems, although it might have done. It did work on most Win98 systems, but
it has never been the preferred way of addressing printers, and I would
strongly suggest that you refrain from using it.

If you want to bypass the Windows driver and send raw data directly to a
printer then you really should be using the OpenPrinter and various other
related functions in winspool.drv. Naturally this, and all other similar
methods, will work only if the printer is actually capable of understanding
the raw data that you send it. Most old fashioned printers, and many new
models as well, understand raw Ascii and various control [Esc] codes but
some modern printers (quite a lot, in fact) simply do not understand that
stuff any longer (especially the cheaper "specially made for Windows"
printers) and in such cases you need to send them stuff that they do
understand (properly produced .prn files for that model, for example). As a
simple example, I currently have two HP inkjet printers. One of them simply
will not accept raw Ascii text at all, whereas my HP PhotoSmart C5280 will
accept it, although it "waits about two minutes" after you have sent the
stuff (even just a few dozen characters) before it finally decides that it
is raw Ascii data you intended to send it and it only prints the page after
those two minutes have elapsed. My older simple dot matrix printers of
course just printed the page immediately, with no problems at all. Anyway,
on the assumption that your own printer understands the stuff you are
attempting to send it (which I assume it must do or your code would never
have worked on that printer at all) then try the following (on a Form with a
Command Button):

Mike

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

Private Sub Command1_Click()
Dim pHandle As Long, retVal As Long
Dim bytesWritten As Long, lDoc As Long
Dim s1 As String, MyDocInfo As DOCINFO
Dim fNum As Long
' send to the printer currently being used by the
' VB Printer Object for test purposes, but you can
' of course send to any other printer.
retVal = OpenPrinter(Printer.DeviceName, pHandle, 0)
If retVal = 0 Then
  MsgBox "Printer Not found"
  Exit Sub
End If
MyDocInfo.pDocName = "Any Name"
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(pHandle, 1, MyDocInfo)
Call StartPagePrinter(pHandle)
fNum = FreeFile
Open "c:\temp\test.txt" For Binary As fNum
s1 = Space$(LOF(fNum))
Get fNum, 1, s1
Close fNum
retVal = WritePrinter(pHandle, ByVal s1, _
    Len(s1), bytesWritten)
retVal = EndPagePrinter(pHandle)
retVal = EndDocPrinter(pHandle)
retVal = ClosePrinter(pHandle)
End Sub
Author
28 Nov 2007 5:27 AM
CWYap
Show quote
"Mike Williams" wrote:

>
> "CWYap" <CW***@discussions.microsoft.com> wrote in message
> news:D8C6B0D3-5C0F-4ADC-AF91-F11D18BEE643@microsoft.com...
>
> > Below is my vb6 code to print file through network shared printer:
> > [big snip]
> > Open sPrintPath For Binary As iLPT
>
> Actually that method (opening printer port or printer device name as a file)
> does not work in Vista even on my locally attached printer (at least on my
> own two Vista machines) and it certainly doesn't work over my "laptop to
> main" simple home network. I'm not so sure that it even worked on many XP
> systems, although it might have done. It did work on most Win98 systems, but
> it has never been the preferred way of addressing printers, and I would
> strongly suggest that you refrain from using it.
>

That's a good code. Thanks for your help.

AddThis Social Bookmark Button