Announcement

Collapse
No announcement yet.

Changing default printer script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing default printer script

    Hi,

    I'm looking for a script that changes the default to another printer if the
    local printer is not installed on the local PC.

    At the moment i have this:

    Code:
    Option Explicit
    
    Dim objNetwork
    
    Set objNetwork = CreateObject("WScript.Network")
    
    Dim WshShell: Set WshShell = CreateObject("Wscript.Shell")
    
    Dim strLocal1, strLocal2, strUNCPrinter1, strUNCPrinter2, strLocal
    strUNCPrinter1 = "\\print-server\HP Universal Printer for accounts"
    strUNCPrinter2 = "\\print-server\Ricoh Aficio 1224C PCL 5c"
    strLocal1 = "\\print-server\HPDesignJet"
    strLocal2 = "\\print-server\DYMO"
    strLocal = "Accounts Printer"
    
    objNetwork.AddWindowsPrinterConnection strUNCPrinter1
    objNetwork.AddWindowsPrinterConnection strUNCPrinter2
    objNetwork.AddWindowsPrinterConnection strLocal1
    objNetwork.AddWindowsPrinterConnection strLocal2
    
    objNetwork.SetDefaultPrinter strLocal
    
    WScript.Quit
    strLocal is the locally installed printer but if it doesn't exist i want the
    default printer to be strUNCPrinter2.

    Please help, many thanks
    Last edited by tonyyeb; 20th February 2007, 14:30. Reason: Code tags used

  • #2
    Re: Changing default printer script

    Try this:

    Code:
    On Error Resume Next
    
    objNetwork.SetDefaultPrinter strLocal
    
    If Err.Number <> 0 Then
    objNetwork.SetDefaultPrinter strUNCPrinter2
    End If On Error GoTo 0
    Note this will not trap an error with strUNCPrinter2. To do that, put an "On Error GoTo 0" after testing Err.Number and before trying strUNCPrinter2.
    Last edited by rvalstar; 20th February 2007, 18:41. Reason: Converted to Code tags
    Cheers,

    Rick

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

    Comment


    • #3
      Re: Changing default printer script

      Many thanks rvalstar. Works a treat.

      Comment


      • #4
        Re: Changing default printer script

        Just wondering if i can take this one step further.

        I hace an accounts package that requires a specific printer to be default to enable it to print.

        Is there any way of creating a script that will run when i open the accounts package executable file that will change the default printer?

        Going on from that would it then be possible to set the default printer again when the program is exited?

        maybe only the executable one may have a solution?

        Cheers

        Comment


        • #5
          Re: Changing default printer script

          If you change the shortcut to run a script then have the script set the default printer for the package, launch the package (waiting for it to close) then set the default printer back. That what you're looking for?

          You need WshShell.Run:

          http://msdn.microsoft.com/library/de...640b0e32ea.asp

          See the second part of example 1 to keep the script waiting for the EXE to close.
          Cheers,

          Rick

          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

          2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

          Comment


          • #6
            Re: Changing default printer script

            looks like it has potential, although i wouldn't know where to start in terms of putting the script together

            The path for the accounts package is "C:\Program Files\Winaccs\winaccs.exe"

            The default printer would be strUNCPrinter2 when Windows loads(this is set by a vb script in thew netlogon file with each user pointing to it). The printer required for the accounts package would be the strLocal but then it would have to go back to strUNCPrinter2 when the program has been exited.

            Thanks for your help with this!

            Chris

            Comment


            • #7
              Re: Changing default printer script

              Come on, you can do this. You're 80% of the way there. Give it a try.

              Make a script that sets the printer, launches Notepad, and sets the printer back after Notepad exits. Test it. Next, substitute your app. Test it. Finally, make a shortcut that calls cscript and passes your VBS. Test it. Deploy it.

              An Admin that can script is far more valuable than one that can't.

              Do post back w/ the outcome and any issues you need help resolving.
              Cheers,

              Rick

              ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

              2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

              Comment


              • #8
                Re: Changing default printer script

                OK, made a bit of progress as far as it sets the default printer and opens notepad but when i close notepad i get an error and it doesn't set the default printer back to the orginal one.

                Line: 15
                Char: 1
                Variable is undefined: 'Return'
                Code: 800A01F4

                Thanks

                Comment


                • #9
                  Re: Changing default printer script

                  sorry, you'll need my code!

                  Code:
                  Option Explicit
                  
                  Dim objNetwork
                  
                  Set objNetwork = CreateObject("WScript.Network")
                  
                  Dim WshShell: Set WshShell = CreateObject("Wscript.Shell")
                  
                  Dim strLocal
                  strLocal = "Accounts Printer"
                  
                  objNetwork.SetDefaultPrinter strLocal
                  
                  Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)
                  Last edited by tonyyeb; 20th February 2007, 14:29. Reason: Used CODE tags

                  Comment


                  • #10
                    Re: Changing default printer script

                    Dim Return
                    Cheers,

                    Rick

                    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                    2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

                    Comment


                    • #11
                      Re: Changing default printer script

                      cheers.

                      how does this script work then? Does it set the default printer and then when the program is quit it then Returns to the orginal default as apposed to changing the default printer when the accounts package opens and then setting a different default printer when it closes.

                      i cant get it to return to the original printer.
                      its probably something small that i've missed out.

                      Code:
                      Option Explicit
                      
                      Dim objNetwork
                      
                      Set objNetwork = CreateObject("WScript.Network")
                      
                      Dim WshShell: Set WshShell = CreateObject("Wscript.Shell")
                      
                      Dim strLocal, strUNCPrinter
                      strLocal = "Accounts Printer"
                      
                      objNetwork.SetDefaultPrinter strLocal
                      
                      Dim Return
                      Return = WshShell.Run("calc " & WScript.ScriptFullName, 1, true)
                      
                      
                      WScript.Quit
                      Last edited by tonyyeb; 20th February 2007, 14:30. Reason: Code tags again

                      Comment


                      • #12
                        Re: Changing default printer script

                        After the Return = ..., I don't see your code to set the printer back. This would be the bit w/ the On Error ... we did at the top of the thread.
                        Cheers,

                        Rick

                        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                        2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

                        Comment


                        • #13
                          Re: Changing default printer script

                          To make it more readable please use the code tags next time..

                          about you're script,


                          Code:
                          Option Explicit
                          
                          Dim objNetwork, WshShell, strLocal, Return
                          
                          Set objNetwork = CreateObject("WScript.Network")
                          Set WshShell = CreateObject("Wscript.Shell")
                          
                          strLocal = "Accounts Printer"
                          objNetwork.SetDefaultPrinter strLocal
                          Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)
                          Marcel
                          Technical Consultant
                          Netherlands
                          http://www.phetios.com
                          http://blog.nessus.nl

                          MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                          "No matter how secure, there is always the human factor."

                          "Enjoy life today, tomorrow may never come."
                          "If you're going through hell, keep going. ~Winston Churchill"

                          Comment


                          • #14
                            Re: Changing default printer script

                            Still playing with this and its still giving me bother.

                            The On Error bit obviosuly needs changing so in continues no matter what. Instead of On Error i need something else.

                            Comment


                            • #15
                              Re: Changing default printer script

                              Originally posted by cmcf View Post
                              Still playing with this and its still giving me bother.

                              The On Error bit obviosuly needs changing so in continues no matter what. Instead of On Error i need something else.
                              Not following you. "On Error Resume Next" will continue no matter what. If you stick one on those at the top then you need to do an Err.Clear followed by whatever you are testing followed by an Err.Number as follows:

                              Code:
                              Option Explicit
                              
                              Dim objNetwork, WshShell, strLocal, strUNCPrinter2, Return
                              
                              On Error Resume Next
                              
                              Set objNetwork = CreateObject("WScript.Network")
                              Set WshShell = CreateObject("Wscript.Shell")
                              
                              strLocal = "Accounts Printer"
                              strUNCPrinter2 = "\\print-server\Ricoh Aficio 1224C PCL 5c"
                              
                              objNetwork.SetDefaultPrinter strLocal
                              
                              Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)
                              
                              Err.Clear
                              
                              objNetwork.SetDefaultPrinter strLocal
                              
                              If Err.Number <> 0 Then
                              objNetwork.SetDefaultPrinter strUNCPrinter2
                              End If WScript.Quit
                              This is a combo of the bits in the thread. Haven't tested it so could be a typo in there but you get the drift.

                              Once you get this bit working, replace Notepad w/ your App.
                              Cheers,

                              Rick

                              ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                              2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

                              Comment

                              Working...
                              X