Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Printing Documents in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Printing Documents in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-07-07

    Table of Contents:
  • Printing Documents in WSH
  • Determining the System Default Printer
  • Setting the System Default Printer
  • Shell Printing and Printing with Notepad
  • Printing Word and PDF Documents
  • Printing Web Documents
  • Printing Web Documents continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Printing Documents in WSH - Setting the System Default Printer


    (Page 3 of 7 )

    Now we’ll take a look at ways of setting the system default printer setting.  This will allow us to control what printer to use in cases when we cannot specify one directly.

    Set WshNetwork = CreateObject("WScript.Network")

    WshNetwork.SetDefaultPrinter(strPrinter)

    The simplest method of setting the default printer relies on the SetDefaultPrinter method of the WshNetwork object.  It accepts a single parameter which is a valid UNC path to the printer to be used.

    The next method uses WMI to set the default printer and is almost exactly the opposite of the script we used to determine the current system setting.

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" & strComputer _

       & "rootcimv2")

     

    Set colInstalledPrinters =  objWMIService.ExecQuery _

       ("Select * from Win32_Printer Where Name = '" & strPrinter & "'")

    In this case, we’re querying the Win32_Printer class for a specific printer.  The strPrinter variable should contain the name of an installed printer exactly as it is listed by the sample script at the beginning of this article.

    Printer names are defined by their drivers.  The driver name is taken from the .inf file used to install the driver.  You can see this name in the Printers and Faxes Control Panel applet or in Device Manager with “Show hidden devices” enabled.

    This WMI query will return a collection of printer objects that should again only have a single member unless there are multiple instances of the same printer installed.

    For Each objPrinter In colInstalledPrinters

       objPrinter.SetDefaultPrinter

    Next

    The printer object then provides the SetDefaultPrinter method, for a quick way of changing its Default property to True.

    You can also set the system default printer by using the system API with the help of rundll32 on the command line.

    Set WshShell = CreateObject("WScript.Shell")

    strCommand = "rundll32 printui.dll,PrintUIEntry /y /n" & strPrinterUNC

    result = WshShell.Exec(strCommand)

    This method uses the WshShell object’s Exec method to run the appropriate command line.  The strPrinterUNC variable should contain a fully qualified UNC path to an installed printer.

    Regardless of whichever method you choose to use, any time you set a printer as the default, any other installed printers will automatically have their default value set to False.

    One final method of setting the default system printer would be to manually write the registry key that you saw previously.  I’m not providing an example of this since it’s best to avoid writing directly to the registry from an unattended script whenever possible.

    More Windows Scripting Articles
    More By Nilpo


       · There's plenty of reason why you might want your script to print documents. Maybe...
     

    WINDOWS SCRIPTING ARTICLES

    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...
    - Modifying XML Files in WSH
    - Reading XML Files in WSH
    - Visual Basic 2005 XML Programming Using XML ...
    - Creating an XML Document in WSH
    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT