Get Selected Printer

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.

You can save the PrinterSetup.SetupString and then restore it later. That will allow you to use the same printer without showing the printer dialog.

I looked into the SetupString, and all I saw was…

DoNotAlterThis=SetupString.2 ActualHorizontalResolution=72 ActualVerticalResolution=72 MaxHorizontalResolution=72 MaxVerticalResolution=72

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.

platform ?

Sorry…
Windows 7 - although I’d prefer a universal (or at least Mac/Windows) solution(s).

On OS X you can get / set various things using LPR or other commands issued via the shell

Not sure you can do that on Windows

Yea… I don’t even know what LPR is. Windows is my first priority at the moment.

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")

dim pagesetup As New PrinterSetup

dim printer as Graphics

printer = OpenPrinter()

if printer = nil then return

dim mStp As StyledTextPrinter

mStp = taSlip.styledTextPrinter(Printer, pagesetup.Width)

mStp.DrawBlock(0,0, pagesetup.Height)[/code]

[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.

Maybe a Work Around For Windows

  1. Open the Printer Property Page of your USB printer
  2. Go to the share / sharing tab. Check the Share button and give the one printer share name, and click on ok.
  3. Open the Command Prompt CMD
  4. 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

  1. Open the Printer Property Page of your USB printer
  2. 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.

Hi Peter Fargo

you code is using a API

Dim b As Boolean

b = SystemInformation.SetDefaultPrinter("\\VADSBS\Kitchen")

Only for Windows!

This should also work if you share your printer:
Display Printers:

[code]Dim objNetwork As OLEObject
Dim objPrinters As OLEObject

Dim nDrives As Integer
Dim outText as String

objNetwork = New OLEObject(“WScript.Network”)
objPrinters = objNetwork.EnumPrinterConnections

nDrives = (objPrinters.Count)

For i as integer = 0 to nDrives -1 Step 2
outText = outText + "Printer Port: " + objPrinters.Item(i) + " = " + objPrinters.Item(i + 1) + EndOfLine.Windows
Next

MsgBox outText

exception err as oleexception
msgbox err.message
[/code]

Select Default Printer:

[code] Dim objNetwork As OLEObject
Dim PrinterPath As variant

PrinterPath = “\\My_Computer_Name\Shared_printer_name”

objNetwork = New OLEObject(“WScript.Network”)
objNetwork.SetDefaultPrinter(PrinterPath)

exception err as oleexception
msgbox err.message[/code]

If you need to know user, domain and hostname:

[code] Dim objNetwork As OLEObject

objNetwork = New OLEObject(“WScript.Network”)

Msgbox "Domain: " + objNetwork.Userdomain +EndOfLine.Windows _

  • "Computer Name: " + objNetwork.ComputerName + EndOfLine.Windows _
  • "User Name: " + objNetwork.UserName

exception err as oleexception
msgbox err.message
[/code]

thanks
works perfect

for such cases rundll is the perfect solution, just execute this line in Shell (DOS-Windows):

RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n “Replace this with your Windows Printer Name

Saving/restoring the printersetup setupstring does work, without having to change the user’s default printer.

Tim is correct as usual and the key is, as he mentioned, the string is binary.

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.

There are 2 problems with your code.

  1. You must call ps.PageSetupDialog to get a valid printer setupstring.
  2. 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.)