Solved

Accessing two printers from a VBScript

Posted on 2010-11-23
4
1,031 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now