Windows Vista Forums

VBScript to print on secondary printer
  1. #1


    drhowarddrfine Guest

    VBScript to print on secondary printer

    I have a script that goes out to the web, retrieves a web page, saves
    it on the desktop and prints it. This works great with the default
    printer but I need it to print on a secondary printer without changing
    the default. Any ideas?
    The only line that was changed from the working version is the one
    after 'enable this to print'.

    'define vars
    dim officePrinter
    officePrinter = "\\server\printer name"
    url = "http://mysite.com"
    path = "C:\Users\Store\Desktop\temp.txt"
    'instantiate
    Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txtfile = fso.OpenTextFile(path, 2, True)
    Set oWS = WScript.CreateObject("WScript.Shell")
    'make request
    xmlhttp.open "GET", url, false
    xmlhttp.send ""
    'wait for response
    xmlhttp.waitForResponse()
    'if status is 200, then it's OK
    if xmlhttp.status = 200 then
    txtfile.WriteLine(xmlhttp.responseText)
    txtfile.Close
    'enable this to print
    oWS.Run "notepad.exe /p " + path + " " + officePrinter
    'enable this to just display
    'oWS.Run "notepad.exe " + path
    else
    'popup bad response, or just omit to end
    WScript.Echo("bad response")
    end if
    'destroy objects. I'm not sure this is necessary
    Set txtfile = nothing
    Set xmlhttp = nothing
    Set oWS = nothing
    Set fso = nothing




      My System SpecsSystem Spec

  2. #2


    drhowarddrfine Guest

    Re: VBScript to print on secondary printer

    Just found out that Windows is such a POS system that it doesn't allow
    scripts to print to anything but the default printer. Unbelievable.

      My System SpecsSystem Spec

  3. #3


    Tom Lavedas Guest

    Re: VBScript to print on secondary printer

    On Dec 16, 7:24*pm, drhowarddrfine <robbel...@xxxxxx> wrote:

    > Just found out that Windows is such a POS system that it doesn't allow
    > scripts to print to anything but the default printer. *Unbelievable.
    Yes, but WMI allows you to reset which printer is the default in a
    script. Then you can send your print job to the default and after the
    job is done, it can be swapped back to the original. For example,
    this code fragment saves the default printer's name and then sets the
    default to another printer (assuming it is installed on that
    machine) ...

    Set oWMISrvc = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\.\root\cimv2")

    Set cDfltPrinter = oWMISrvc.ExecQuery _
    ("Select * from Win32_Printer Where Default = True")

    For Each objPrinter in cDfltPrinter
    if objPrinter.Default then sDfltPtr = objPrinter.Name
    Next

    sAltPrtrName = "\\\\server\\printer"

    Set cPrinter = oWMISrvc.ExecQuery _
    ("Select * from Win32_Printer Where Name = '" _
    & sAltPrtrName & "'")

    For Each oPrtr in cPrinter
    oPrtr.SetDefaultPrinter()
    Next

    Set cDfltPrinter = oWMISrvc.ExecQuery _
    ("Select * from Win32_Printer Where Default = True")

    For Each objPrinter in cDfltPrinter
    if objPrinter.Default then sNewDfltPtr = objPrinter.Name
    Next

    wsh.echo "Default was", sOldDfltPtr
    wsh.echo "Default is now", sNewDfltPtr

    Just reverse the process to restore the original default after you
    send the print job. SOme error detection is probably in order, but I
    leave that to the you to work out.

    Tom Lavedas
    ***********
    http://there.is.no.more/tglbatch/


      My System SpecsSystem Spec

VBScript to print on secondary printer problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
[MS Word Macro] VBscript flag image to not print it marnick.vos VB Script 2 30 Mar 2009
HTML and VBscript printer management page paultown VB Script 2 15 Jan 2009
Powershell Wrapper Script Problems - Trying to Call PowershellExchange 2007 Commands from Secondary Language (Like VBScript) Nathan PowerShell 0 03 Sep 2008
Vista Can't Print to Win2K shared printer-Test Print disappears fr Jeremy Vista networking & sharing 2 29 Jun 2008
Re: Print Shop won't print using laser printer Cari \(MS-MVP\) Vista print fax & scan 0 30 Mar 2007