Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Accessing two printers from a VBScript

Posted on 2010-11-23
4
1,035 Views
Last Modified: 2012-05-10
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
Comment
Question by:RandyTommy
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
MaxSoullard earned 500 total points
ID: 34203453
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
 

Author Comment

by:RandyTommy
ID: 34214652
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
 
LVL 5

Expert Comment

by:MaxSoullard
ID: 34216137
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
 

Author Comment

by:RandyTommy
ID: 34224063
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question