Solved

Accessing two printers from a VBScript

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Script is being strange 8 69
execution of the windows script host failed. (access is denied.) 3 115
Problem to With line 4 57
Update Access FrontEnd by Version # 9 43
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…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

777 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