Click here to Skip to main content
15,040,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used to code using vb6 but 2 years go started using vb.net and others.

I am programming a retail sales system that should print on 4 types of printers, Receipt, A4, A5, and Label.

my problem is that I need to print the receipt without changing the default printer, I can print normally if the printer is set to default.

I tried to chnage it to default for printing and then return to the original one after printing and maybe that is good but slowing the code somehow.

What makes me wonder is I already mentioned the name of the printer that I want to print using it to CrystalReport but it prints to the default, I'll show some codes:

Private Sub PrintFromDS()
Dim rptDoc As New ReportDocument
rptDoc = New CRrcpt
Dim i As Integer
Dim doctoprint As New System.Drawing.Printing.PrintDocument()
doctoprint.PrinterSettings.PrinterName = RcptPrntr '"Star TSP100 Cutter (TSP143)"
Dim rawKind As Integer
For i = 0 To doctoprint.PrinterSettings.PaperSizes.Count - 1
If doctoprint.PrinterSettings.PaperSizes(i).PaperName = "72mm x Receipt" Then
rawKind = CInt(doctoprint.PrinterSettings.PaperSizes(i).GetType().GetField("kind", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes(i)))
Exit For
End If
Next
rptDoc.SetDataSource(ds)
rptDoc.PrintOptions.PaperSize = CType(rawKind, CrystalDecisions.Shared.PaperSize)
rptDoc.PrintToPrinter(1, False, 0, 0)
rptDoc.Refresh()
End If
End Sub

The above code is working great but what about the following line: doctoprint.PrinterSettings.PrinterName = RcptPrntr

Why the it prints to the default?


I used the following function to set the default printer and maybe I would use it again to set the original printer but I don't understand how it works and I don't know how much should I trust this function when my software is used on client pc

<dllimport("winspool.drv", charset:="CharSet.Auto," setlasterror:="True)">
Public Function SetDefaultPrinter(Name As String) As Boolean
End Function

Nothing inside the function but it is working, how that? If I enter one empty line as below:

<dllimport("winspool.drv", charset:="CharSet.Auto," setlasterror:="True)">

Public Function SetDefaultPrinter(Name As String) As Boolean
End Function

Then vb shows a warning and an error Error 1 Attribute specifier is not a complete statement. Use a line continuation to apply the attribute to the following statement. D:\Development\VB.Net\Projects\x\x\Modules\modPrinter.vb

Warning 2 Function 'SetDefaultPrinter' doesn't return a value on all code paths. Are you missing a 'Return' statement? D:\Development\VB.Net\Projects\x\x\Modules\modPrinter.vb

Why, what is connection between that line and the function.?

Finally,

While I'm seraching I found the following:

For each Printer in Printers
next

I used this in vb6 but many people using it in .net when I try to use it it shows error

Error 1 'Printers' is not declared. It may be inaccessible due to its protection level. D:\Development\VB.Net\Projects\x\x\Modules\modPrinter.vb

I need to know what is happening in all cases

The main problem is how to print directly to the desired printer no problem for changing the printer and return to original but PERFORMANCE and RELIABILITY are very important.

if that can be done internally without depending on windows then it will be good. Please give me solution not just answers as I tried a lot of things.

Thank you in advance.

What I have tried:

<DllImport("winspool.drv", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Function SetDefaultPrinter(Name As String) As Boolean
End Function
Posted
Updated 28-Aug-21 23:11pm

1 solution

Hi,
Microsoft provide code to write directly to printers, useful when you want to line print and send escape sequences to control the printer directly rather than page print.

My example;

Print Direct To Windows Printer (EPOS Receipt)[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900