Solved

Accessing two printers from a VBScript

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 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

15 Experts available now in Live!

Get 1:1 Help Now