[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1044
  • Last Modified:

Accessing two printers from a VBScript

I'm using a VBScript to print to two printers, one prints the reciept, and the second prints a label to stick on the merchandice.  The line I use to print is:

call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)

However, it only prints to the default printer, with no way to name the second printer.
I need a way to auto-change the default printer between the two printings.

Another listing here gave the below as a solution.  Their code runs, but does not change the default printer.  I hope you have a correction, or better idea for me.  :)

I'm using Windows-7 with IE8.  Here's the code that didn't work for me:

Const HKEY_CURRENT_USER = &H80000001
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
      strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Internet Explorer\PageSetup"
strValueName = "printer"
szValue = "Microsoft XPS Document Writer"
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, szValue
0
RandyTommy
Asked:
RandyTommy
  • 2
  • 2
1 Solution
 
MaxSoullardCommented:
This is the vb code to set the default printe if they are network printersr:

Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.SetDefaultPrinter "\\PrintServer1\PrinterName"


And this is the vb code to se the default printer if local:


strPrinterName = "PrinterName" 'as displayed on the printer and fax window
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = '" & strPrinterName & "'")
For Each objPrinter in colInstalledPrinters
    objPrinter.SetDefaultPrinter()
Next
0
 
RandyTommyAuthor Commented:
WOW!  I'm pleasantly surprised how fast you answered, and that your code is exactly what I asked for, and works perfectly.  You've earned your 500 points already!

But it also showed me I misunderstood the problem a little.  Turns out, the default printer setting only selects a printer as the window opens.  

After that, changing the default printer programatically does not change the printer selected for that window.

A work-around is to open a first window with the first printer selected.  Then change the default to the second printer as you explained above.  

Then open a child window from the first (parent) window.  The result is that the parent window will print to the first printer, and javascript

in the parent window can instruct the child window to print to the second printer as needed.

But can we make this simpler?  Is there a way for VBScript to change the active "selected" printer in a single window so the window can operate

both printers without need of a "child" window?
0
 
MaxSoullardCommented:
The script can control the Windows default printer, when a software is open it takes this printer as default and then the software controls de seletion of the default printer. It will be really difficult to change the default printer once the software is already open.
0
 
RandyTommyAuthor Commented:
Ok.  I'll stick with the two-window method I described above.  It does the job smoothly.

I'll accept that there is no way to allow a web page to change the active printer.  In another blog, an author states he's checked with experts who told him "No", and that even microsoft's own literature states changing the active printer to be an impossibility.

However I find your code to change the default printer works at any time.  (Too bad the active printer doesn't change along with the defaut setting, instead of only as the window opens).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now