Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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 - Determining the System Default Printer


    (Page 2 of 7 )

    The system default printer setting is stored in the local hardware profile in the Windows registry.  This is a global setting that is used by any application that relies on system APIs for printing.  Since this setting is stored in the registry, it becomes the first logical place to look for the current setting.

    Set WshShell = CreateObject("WScript.Shell")

    strRegVal = "HKCUSoftwareMicrosoftWindows NTCurrentVersionWindowsDevice"

     

    strDefault = WshShell.RegRead(strRegVal)

    strDefault = Left(strDefault, InStr(strDefault, ",") - 1)

    You can use any method you like to read the required registry key.  For simplicity, I’ve used the WshShell object to read the registry, but I could have just as easily used WMI.  The key should contain something like the following:

    Lexmark 3500-4500 Series,winspool,Ne04:

    As you can see, the printer name is the first of a series of comma-delimited values.  Making use of VBScript’s InStr and Left functions allows us to parse out only the required information.

    Once you have the printer name, you can construct a UNC path to the printer in the form of computernameprintername.  The WshNetwork object can be used to retrieve the local computer name.

    The next method for retrieving the system default printer setting relies on WMI.

    strComputer = "."

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

       & "rootcimv2")

    Set colPrinters = objWMIService.ExecQuery(_

       "Select * From Win32_Printer Where Default = '-1'")

    After connecting to the local WMI service’s rootcimv2 namespace, we query the Win32_Printer class for all instances where the Default property is set to -1.  Keep in mind that -1 is the integer representation of a Boolean true value.  This query returns a collection of printer objects that have the Default value set to True.

    For Each objPrinter In colPrinters

       strDevice = objPrinter.DeviceID

       strSystem = objPrinter.SystemName

    Next

    Next we’ll use a For…Next loop to iterate through the collection and read the DeviceID and SystemName properties for each printer object.  This may seem a bit redundant since there can only be one default printer, meaning that the colPrinters collection should only contain a single reference.  This is a valid argument, so if you prefer you can use this code instead.

    strDevice = colPrinters.Item(0).DeviceID

    strSystem = colPrinters.Item(0).SystemName

    My only argument against this more direct method is that it will cause an error if it is run on a system that does not have any installed printers.  In that case, the colPrinters collection will not have any members and an error will be raised when trying to access a non-existent item.

    Whichever method you choose to use, you will end up with the same result.  The DeviceID property will return the name of the printer and the SystemName property will return the name of the system that the device is installed on—the current computer name.

    If Not IsEmpty(strDevice) Or Not IsEmpty(strSystem) Then

       strUNC = "" & strSystem & "" & strDevice

    Else

       strUNC = ""

    End If

    Building a valid UNC path is as simple as concatenating our two variables with the proper forward slashes.  I’ve chosen to do this inside of an If block to again avoid errors in the scenario mentioned previously.

    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 5 hosted by Hostway
    Stay green...Green IT