|
Prev: Editing Controls at runtime? Creating Code at Runtime? Creating EXE Files at Runtime?
Next: Convert image to text
From: John Simpson on 18 Jun 2008 14:38 Please help!!!! Environment is VB6 with Windoze XP Professional. I have a timeclock application at one of my sites that prints a line of data and writes to a database whenever an employee 'punches' in or out or off of a job. Right now the printer is local, and connected to the parallel port on the server. A new server is about to be put in place, and it has no parallel port, so the printing will have to be done either to a network printer, or to a printer connected to the server USB port. Here lies the problem! My application is not using the printer object because I can't tolerate a page feed after each punch. The current code is: open sPrinterName for append as #1 ' sPrinterName is a printer defined on the server (ML490) write #1, sPunchData ' time, date, employee clock #, station, job #, punch code, etc close #1 ' yeah, I know ---- use FreeFile This works fine with a local printer, connected to the lpt1 port, but prints nothing using a network or USB printer, instead, it creates a file named ML490. Two questions: 1). Is there a way to use the printer object to print a line of data without using printer.enddoc, or with printer.enddoc without the page feed? 2). How can I define sPrinterName so I can use the open, write, and close scheme with a network or USB printer? I've tried \\server-name\printer-name, but I'm getting Run-time-error 75. I can overcome the page feed problem by creating a string array of data and issuing the printer.enddoc after 60 or so punches, but I don't think this will be acceptable to the customer. Any insight will be gratefully received!!! John
From: Mike Williams on 19 Jun 2008 02:12 On 18 Jun, 19:38, "John Simpson" <jas...(a)earthlink.net> wrote: > Right now the printer is local, and connected to > the parallel port on the server. A new server is > about to be put in place, and it has no parallel > port, so the printing will have to be done either > to a network printer, or to a printer connected to > the server USB port. Here lies the problem! My > application is not using the printer object because > I can't tolerate a page feed after each punch. The > current code [snipped] works fine with a local > printer, connected to the lpt1 port, but prints > nothing using a network or USB printer You need to use Windows winspool driver to write raw data to printers that are not connected to the parallel port. Also, to make it work in "line by line" mode you actually need to open the printer and send the line of text and then close the printer for each line (otherwise the data will not be sent each time), but do not send a page feed until you want the actual page to be ejected. Also, don't forget that some modern printers will not actually work well in the "line by line" mode, whatever you do (although many will). They will actually work, including USB connected printers, and will draw in the page ready for printing as soon as you send the first line, but on some of them the line will not actually be printed until the page gets full. On others though it will work fine, in the standard "old fashioned way" with each line being actually printed and the page being advanced by one line every time. For example, my Epson Stylus USB connected printer works fine in this mode, just like an old fashioned impact dot matrix, but my HP 2575 multi function printer refuses to actually put any ink on the page or to advance a line until you have printed sufficient lines to fill the page, or of course until you send a page feed. Anyway, try the following example. Paste it into a VB Form containing two Command Buttons. This example prints to the system default printer (the one being pointed at by the VB printer object) but you can of course print to any other printer simply by using the appropriate device name. Mike Option Explicit Private Type DOCINFO pDocName As String pOutputFile As String pDatatype As String End Type 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 Sub Form_Load() Command1.Caption = "Print Line" Command2.Caption = "Eject page" Caption = "Press Print Line 20 times then press Eject Page" End Sub Private Sub Command1_Click() Dim lhPrinter As Long, retVal As Long Dim lpcWritten As Long, lDoc As Long Dim sWrittenData As String, MyDocInfo As DOCINFO Static z As Long retVal = OpenPrinter(Printer.DeviceName, lhPrinter, 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(lhPrinter, 1, MyDocInfo) Call StartPagePrinter(lhPrinter) z = z + 1 sWrittenData = "Does line " & Format(z) _ & " get printed?" & vbCrLf retVal = WritePrinter(lhPrinter, ByVal sWrittenData, _ Len(sWrittenData), lpcWritten) retVal = EndPagePrinter(lhPrinter) retVal = EndDocPrinter(lhPrinter) retVal = ClosePrinter(lhPrinter) End Sub Private Sub Command2_Click() Dim lhPrinter As Long, retVal As Long Dim lpcWritten As Long, lDoc As Long Dim sWrittenData As String, MyDocInfo As DOCINFO retVal = OpenPrinter(Printer.DeviceName, lhPrinter, 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(lhPrinter, 1, MyDocInfo) Call StartPagePrinter(lhPrinter) sWrittenData = vbFormFeed retVal = WritePrinter(lhPrinter, ByVal sWrittenData, _ Len(sWrittenData), lpcWritten) retVal = EndPagePrinter(lhPrinter) retVal = EndDocPrinter(lhPrinter) retVal = ClosePrinter(lhPrinter) End Sub
From: John Simpson on 20 Jun 2008 17:02 "Mike Williams" <gagamomo(a)yahoo.co.uk> wrote in message news:79de0327-6534-45cb-baf1-1767e52d1b96(a)34g2000hsh.googlegroups.com... > On 18 Jun, 19:38, "John Simpson" <jas...(a)earthlink.net> wrote: > >> Right now the printer is local, and connected to >> the parallel port on the server. A new server is >> about to be put in place, and it has no parallel >> port, so the printing will have to be done either >> to a network printer, or to a printer connected to >> the server USB port. Here lies the problem! My >> application is not using the printer object because >> I can't tolerate a page feed after each punch. The >> current code [snipped] works fine with a local >> printer, connected to the lpt1 port, but prints >> nothing using a network or USB printer > > You need to use Windows winspool driver to write raw data to printers > that are not connected to the parallel port. Also, to make it work in > "line by line" mode you actually need to open the printer and send the > line of text and then close the printer for each line (otherwise the > data will not be sent each time), but do not send a page feed until > you want the actual page to be ejected. Also, don't forget that some > modern printers will not actually work well in the "line by line" > mode, whatever you do (although many will). They will actually work, > including USB connected printers, and will draw in the page ready for > printing as soon as you send the first line, but on some of them the > line will not actually be printed until the page gets full. On others > though it will work fine, in the standard "old fashioned way" with > each line being actually printed and the page being advanced by one > line every time. For example, my Epson Stylus USB connected printer > works fine in this mode, just like an old fashioned impact dot matrix, > but my HP 2575 multi function printer refuses to actually put any ink > on the page or to advance a line until you have printed sufficient > lines to fill the page, or of course until you send a page feed. > > Anyway, try the following example. Paste it into a VB Form containing > two Command Buttons. This example prints to the system default printer > (the one being pointed at by the VB printer object) but you can of > course print to any other printer simply by using the appropriate > device name. > > Mike > > Option Explicit > Private Type DOCINFO > pDocName As String > pOutputFile As String > pDatatype As String > End Type > 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 Sub Form_Load() > Command1.Caption = "Print Line" > Command2.Caption = "Eject page" > Caption = "Press Print Line 20 times then press Eject Page" > End Sub > > Private Sub Command1_Click() > Dim lhPrinter As Long, retVal As Long > Dim lpcWritten As Long, lDoc As Long > Dim sWrittenData As String, MyDocInfo As DOCINFO > Static z As Long > retVal = OpenPrinter(Printer.DeviceName, lhPrinter, 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(lhPrinter, 1, MyDocInfo) > Call StartPagePrinter(lhPrinter) > z = z + 1 > sWrittenData = "Does line " & Format(z) _ > & " get printed?" & vbCrLf > retVal = WritePrinter(lhPrinter, ByVal sWrittenData, _ > Len(sWrittenData), lpcWritten) > retVal = EndPagePrinter(lhPrinter) > retVal = EndDocPrinter(lhPrinter) > retVal = ClosePrinter(lhPrinter) > End Sub > > Private Sub Command2_Click() > Dim lhPrinter As Long, retVal As Long > Dim lpcWritten As Long, lDoc As Long > Dim sWrittenData As String, MyDocInfo As DOCINFO > retVal = OpenPrinter(Printer.DeviceName, lhPrinter, 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(lhPrinter, 1, MyDocInfo) > Call StartPagePrinter(lhPrinter) > sWrittenData = vbFormFeed > retVal = WritePrinter(lhPrinter, ByVal sWrittenData, _ > Len(sWrittenData), lpcWritten) > retVal = EndPagePrinter(lhPrinter) > retVal = EndDocPrinter(lhPrinter) > retVal = ClosePrinter(lhPrinter) > End Sub > Thanks Mike, that did the trick. Works great with the Okidata dot matrix printer, which is all I need. One last question...... This is good using the default printer. How do I define a printer other than the default?? Google didn't help on this one. John
From: Mike Williams on 20 Jun 2008 18:27 On 20 Jun, 22:02, "John Simpson" <jas...(a)earthlink.net> wrote: > Thanks Mike, that did the trick. Works great > with the Okidata dot matrix printer, which is > all I need. You're very welcome. > One last question...... This is good using the > default printer. How do I define a printer other > than the default?? Google didn't help on this one. You just need to provide the DeviceName of the desired printer in the code. For example, the sample I posted includes the line: retVal = OpenPrinter(Printer.DeviceName, lhPrinter, 0) The above code opens the current default Printer by using the DeviceName of the current VB Printer object. If you instead want to use some other printer then just substitute the actual devicename of the printer you want to use, for example: retVal = OpenPrinter("Canon Inkjet MP360 Series", lhPrinter, 0) If you're not sure what printers are available on the system (or what their DeviceNames are) then you can write some VB code to find them. As an example, the following VB code will allow your user to select a printer. It does so by examining the DeviceNames of all printers that are available on the system and presenting them to the user in a Combo Box, allowing him to chose the printer he wishes to use. There are two blocks of code, one for Form1 (the main Form of your project) and another for Form2 (a second Form in your project). Form1 shoukd contain one Command Button (called "Select Printer if you wish") and the second Form should contain a ComboBox and a Coimmand Button (with the Command Button being called "Okay" or something similar). When the project is run and you click the button on ther main Form then it should show the second Form, allowing you to select a printer. When you click the button on the second Form you will see the DeviceName of the chosen prinbter displayed in the main Form's Caption Bar. Anyway, here's the code. Let me know if you have any problems with it and I'll help you out. Mike ' *** Start of Form 1 Code *** Option Explicit Private Sub Command1_Click() Form2.Show vbModal Me.Caption = Printer.DeviceName End Sub ' *** End of Form 1 Code *** ' ' *** Start of Form 2 Code *** Option Explicit Private Sub Combo1_Click() Caption = Val(Caption) + 1 End Sub Private Sub Form_Activate() Dim printerobject As Printer Dim element As Integer Combo1.Clear For Each printerobject In Printers Combo1.AddItem printerobject.DeviceName If Printer.DeviceName = printerobject.DeviceName Then Combo1.ListIndex = element End If element = element + 1 Next printerobject End Sub Private Sub Command1_Click() ' Set the printer for this application only Set Printer = Printers(Combo1.ListIndex) Form2.Hide End Sub ' *** End of Form 2 Code ***
From: John Simpson on 24 Jun 2008 10:37
"Mike Williams" <gagamomo(a)yahoo.co.uk> wrote in message news:42092c8e-6836-4581-99fc-e7ee0eeb6db9(a)s50g2000hsb.googlegroups.com... > On 20 Jun, 22:02, "John Simpson" <jas...(a)earthlink.net> wrote: > >> Thanks Mike, that did the trick. Works great >> with the Okidata dot matrix printer, which is >> all I need. > > You're very welcome. > >> One last question...... This is good using the >> default printer. How do I define a printer other >> than the default?? Google didn't help on this one. > > You just need to provide the DeviceName of the desired printer in the > code. For example, the sample I posted includes the line: > > retVal = OpenPrinter(Printer.DeviceName, lhPrinter, 0) > > The above code opens the current default Printer by using the > DeviceName of the current VB Printer object. If you instead want to > use some other printer then just substitute the actual devicename of > the printer you want to use, for example: > > retVal = OpenPrinter("Canon Inkjet MP360 Series", lhPrinter, 0) > > If you're not sure what printers are available on the system (or what > their DeviceNames are) then you can write some VB code to find them. > As an example, the following VB code will allow your user to select a > printer. It does so by examining the DeviceNames of all printers that > are available on the system and presenting them to the user in a Combo > Box, allowing him to chose the printer he wishes to use. There are two > blocks of code, one for Form1 (the main Form of your project) and > another for Form2 (a second Form in your project). Form1 shoukd > contain one Command Button (called "Select Printer if you wish") and > the second Form should contain a ComboBox and a Coimmand Button (with > the Command Button being called "Okay" or something similar). When the > project is run and you click the button on ther main Form then it > should show the second Form, allowing you to select a printer. When > you click the button on the second Form you will see the DeviceName of > the chosen prinbter displayed in the main Form's Caption Bar. Anyway, > here's the code. Let me know if you have any problems with it and I'll > help you out. > > Mike > > ' *** Start of Form 1 Code *** > Option Explicit > > Private Sub Command1_Click() > Form2.Show vbModal > Me.Caption = Printer.DeviceName > End Sub > ' *** End of Form 1 Code *** > ' > ' *** Start of Form 2 Code *** > Option Explicit > > Private Sub Combo1_Click() > Caption = Val(Caption) + 1 > End Sub > > Private Sub Form_Activate() > Dim printerobject As Printer > Dim element As Integer > Combo1.Clear > For Each printerobject In Printers > Combo1.AddItem printerobject.DeviceName > If Printer.DeviceName = printerobject.DeviceName Then > Combo1.ListIndex = element > End If > element = element + 1 > Next printerobject > End Sub > > Private Sub Command1_Click() > ' Set the printer for this application only > Set Printer = Printers(Combo1.ListIndex) > Form2.Hide > End Sub > ' *** End of Form 2 Code *** > > Thanks Mike. I appreciate your time and effort. John |