Print to network printer vb
:duck: I can print using vb to my local lpt1, using the code below.
My problem is that now i need to print to a network printer - i need to specify printer name instead of the lpt1.
How can i do this?
VB Code:
Open "lpt1" For Output As #1
For i = 1 To txtPrintNo
Print #1, strText
Next i
Re: Print to network printer vb
You must use the PRINTER object to print to a network printer.
You can loop through the PRINTERS collection looking for the desired printer...
Something like this:
Code:
Dim prtPrinter as Printer
For Each prtPrinter In Printers
If prtPrinter.DeviceName = strDesiredPrinter Then
Set Printer = prtPrinter
Exit For
End If
Next
Once you SET PRINTER - then you use the PRINTER object you just assigned to output to that printer.
Re: Print to network printer vb
Is the network printer installed on your machine?
Re: Print to network printer vb
Re: Print to network printer vb
szlamany has already posted what I was going to post about switching printers (providing it was installed).
Re: Print to network printer vb
Sorry for being dumb but i can't get this working.
here is my code at present - any ideas?
VB Code:
If printerfound Then
j = Printer.DeviceName
intFile = FreeFile()
' Read Label Template
Open "c:\label\label.txt" For Input As #intFile
strText = Input$(LOF(intFile), intFile)
Close #intFile
' Populate with required values
strText = Replace$(strText, "@MASTERPART@", cboPartNumber.Text)
' Print the required number of Labels
Open "lpt1" For Output As #1
For i = 1 To txtPrintNo
Print #1, strText
Next i
Close #intFile
Else
MsgBox ("Printer not found.")
End If
Dim prtPrinter As Printer
For Each prtPrinter In Printers
If prtPrinter.DeviceName = strDesiredPrinter Then
Set Printer = prtPrinter
Exit For
End If
Next
End Sub
Re: Print to network printer vb
You appear to be already using the PRINTER.DEVICENAME at the top of that code - which I'm guessing means you have already determined that the default printer is ok to use.
This code:
Code:
' Print the required number of Labels
Open "lpt1" For Output As #1
For i = 1 To txtPrintNo
Print #1, strText
Next i
Close #intFile
Changes to:
Code:
For i = 1 to txtPrintNo
Printer.Print strText
Next i
Printer.EndDoc
The loop I gave you was for searching the printer collection for the proper printer - assuming it was not the default printer.
Hope this helps!
Re: Print to network printer vb
When i try this the printer icon flickers as though it were in a loop.
Nothing comes out.
thanks for ur help...