How do I find out what the selected USB printer is when using OpenPrinterDialog? And how do I use that specific printer in future print jobs?
The intent is that I wish to retain the printer information and then automatically apply it so that the user doesn’t always have to select the printer before printing.
I don’t see any info in the above related to which printer to use. Plus, I tried it and it didn’t use the correct printer.
I tried to figure out how to do this from the PrinterSetup example provided in the Help, but that was based on PageSetup, which seemed rather irrelevant (and useless) to me. I’ve spent hours on this and I must be missing something as I can’t find any examples for this common functionality.
This is what I’ve tried…
public variable:
msPrinterSetup As String
button1:
[code]Dim g As Graphics
Dim ps As PrinterSetup
ps = new PrinterSetup
if ps <> nil then
g = OpenPrinterDialog
if g <> nil then
msPrinterSetup = ps.SetupString
else
MsgBox “Example not printed due to cancelling Printer Dialog”
end if
else
MsgBox “Example not printed, unable to create new PrinterSetup class”
end if[/code]
button2:
[code]Dim g As Graphics
Dim ps As PrinterSetup
ps = new PrinterSetup
if ps <> nil then
ps.SetupString = msPrinterSetup
g = OpenPrinter(ps)
if g <> nil then
g.DrawString(TextArea1.Text, 10, 10)
else
MsgBox “Example not printed due to cancelling Printer Dialog”
end if
else
MsgBox “Example not printed, unable to create new PrinterSetup class”
end if[/code]
The problem I am having with button1 is that the OpenPrinterDialog still forces the printer (a receipt printer) to print and cut even though I haven’t used “DrawString”. A less important issue (that I can live with) is that it shows the “Print” button, where I’d prefer a “Select” button, since I’s rather not print, but just select the printer in the program’s settings.
The problem I am having with button2 is that it doesn’t use the selected printer. It goes straight to the default printer instead. Which appears to verify my original concern - that SetupString doesn’t store the printer name/id.
The Windows Functionality Suite has a SystemInformation module that has GetDefaultPrinter and SetDefaultPrinter methods. I use these to change printers in Windows XP/7 to avoid dialogs. They work fine with Start receipt printers - both networked and local.
example:
[code] Dim b As Boolean
b = SystemInformation.SetDefaultPrinter("\\VADSBS\Kitchen")
[quote=37664:@Mark Lewis]The problem I am having with button1 is that the OpenPrinterDialog still forces the printer (a receipt printer) to print and cut even though I haven’t used “DrawString”. A less important issue (that I can live with) is that it shows the “Print” button, where I’d prefer a “Select” button, since I’s rather not print, but just select the printer in the program’s settings.
[/quote]
There seems to be no way around this on Windows 7.
The setupstring is binary (and quite long). View the contents in the debugger as hex, you will see the printer name toward the end of the string. It’s there and this works quite well for me on Windows 7, so I’m not sure where the bug is in your code.
Open the Printer Property Page of your USB printer
Go to the share / sharing tab. Check the Share button and give the one printer share name, and click on ok.
Open the Command Prompt CMD
Excute the given command: net use lpt1: \\computername\printersharename /persistent:yes
After you have done that, you can easily run the dos command.
Print MyFile.txt
Or
Open the Printer Property Page of your USB printer
Go to the share / sharing tab. Check the Share button and give the one printer share name, and click on ok.
Print /D:\\computername\printersharename MyFile.txt
Just run the print comand from a Xojo Shell
Some printers can also take a pdf files. If not you will get lot of strange characters printed on your paper.
Tim:
Ok. So if it works then how come the code I posted (third post from top) doesn’t work? It’s pretty basic stuff. I’ll summarize here…
button1 selects a printer that isn’t the default printer. Yet button2 always prints to the default printer.
public variable:
msPrinterSetup As String
button1:
[code]Dim g As Graphics
Dim ps As PrinterSetup
ps = new PrinterSetup
if ps <> nil then
g = OpenPrinterDialog
if g <> nil then
msPrinterSetup = ps.SetupString
else
. . .[/code]
button2:
[code]Dim g As Graphics
Dim ps As PrinterSetup
ps = new PrinterSetup
if ps <> nil then
ps.SetupString = msPrinterSetup
g = OpenPrinter(ps)
if g <> nil then
g.DrawString(TextArea1.Text, 10, 10) ’ Contains a couple lines of text
else
. . .[/code]
Also, I checked the SetupString in Binary and, aside from the small amount of info listed at the top, it’s just a list of empty lines that extends beyond the viewable area of my screen.
Others:
To clarify, I do not want to change the default printer.
You must call ps.PageSetupDialog to get a valid printer setupstring.
You must pass ps to OpenPrinterDialog in order for it to set the printer in ps.setupstring.
Button1:
Dim g As Graphics
Dim ps As PrinterSetup
ps = new PrinterSetup
call ps.PageSetupDialog
if ps <> nil then
g = OpenPrinterDialog(ps)
if g <> nil then
msPrinterSetup = ps.SetupString
end
end
Button2 still uses the default printer. Using PageSetupDialog didn’t appear to have an effect.
Also, in Button1, if I add the line… g.DrawString(TextArea1.Text, 10, 10) … after… if g <> nil
the selected printer works fine (even without calling PageSetupDialog).
The problem is, in a real world POS application, I can’t be asking for additional user interaction to select a receipt printer after each transaction (and I can’t make it the default printer because the program uses 3 different printers). Therefore, I can’t pass the PrinterSetup - I have to create a new instance.
In the example I created, changing your code as above, Button2 uses whatever printer I selected in Button1. Not sure what you’re missing here. I store the setupstring in a database and reuse it without user interaction over and over. Basically, I have the user go through the dialogs once, save the setupstring, and never ask again. (Unless they select an option to change the printer setup.)