Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Other Programming > VBScript
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 30th, 2004, 01:40 PM
Registered User
 
Join Date: Apr 2004
Location: CHicago, il, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dddane
Default Determining what default printer is in VBS/WSH

Determining what default printer is in VBS/WSH
I'm trying to map each user's printers to a new print server with their login script in WSH.

I have the code (below) to successfully do this. However, the ONLY thing missing is setting the default printer. I know that there is a method that DOES set the default printer, but I first need to know what their old default printer was. I haven't been able to find this. I found (in other wrox forums) code in VB that will use API calls to do this--but AFAIK these won't work from WSH.

The code I have (slightly sloppy, I know, but it works) is this:

Code:
 
oldPrinter="\\PRSRV1\Color4002"
newPrinter="\\PRSRV2\Color4002"

For i = 0 to oPrinters.Count - 1 Step 2

if Trim(oPrinters.Item(i+1)) = oldPrinter then
'Printer found. Lets add the replacement, if successful delete the old. 

WshNetwork.AddWindowsPrinterConnection newPrinter

If Err 0 OR blnError = True Then
'Printer was NOT added

Else 

WshNetwork.RemovePrinterConnection oldPrinter, true, true 
End If 'end if err0

End If 'end if printer matches current

Next

'END ITERATION SEARCHING FOR PRINTER
I do this for each printer we have on our network. I don't worry about specifying drivers, as I made a point to use the same driver version on both servers (for anyone copying, this may create an issue if the new print server has a different driver and the user it is being added from does not have local administrative rights). Each user has 5-10 printers that are actually remapped.

Any help on this? Thanks
-Dane

Reply With Quote
  #2 (permalink)  
Old April 30th, 2004, 02:08 PM
Registered User
 
Join Date: Apr 2004
Location: CHicago, il, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dddane
Default

I posted this on another site and got an answer... The completed/working script (for those who care!) is as follows:


Function GetDefaultPrinter()
Set oShell = CreateObject("WScript.Shell")
sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
sDefault = ""
On Error Resume Next
 sDefault = oShell.RegRead(sRegVal)
 sDefault = Left(sDefault ,InStr(sDefault, ",") - 1)
 On Error Goto 0
GetDefaultPrinter = sDefault
End Function

oldDefault = GetDefaultPrinter
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

                           'loop through each printer, searching for printer to replace. 'do this whole iteration one time for EACH printer to replace '################################################# ###########################

    oldPrinter="\\BANNER\Color1100"
       newPrinter="\\CHISALPRSRV\Color1100"

       For i = 0 to oPrinters.Count - 1 Step 2

            if Trim(oPrinters.Item(i+1)) = oldPrinter then
            'Printer found. Lets add the replacement, if successful delete the old.

                WshNetwork.AddWindowsPrinterConnection newPrinter

            if Trim(oldDefault)=Trim(oldPrinter) then
            'set new as default printer
            WshNetwork.SetDefaultPrinter newPrinter
            end if

                If Err <> 0 OR blnError = True Then
                'Printer was NOT added
                    WScript.Echo "UTOH! " & Err
                Else

                    WshNetwork.RemovePrinterConnection oldPrinter, true, true
            End If 'end if err<>0

            End If 'end if printer matches current


         Next

        'END ITERATION SEARCHING FOR PRINTER



                     'END ITERATION SEARCHING FOR PRINTER 'REPEAT THE FOR I=0 THROUGH NEXT FOR EACH PRINTER

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default printer anukagni Access 3 March 21st, 2006 08:10 AM
Function for Finding Default Printer smartgir Classic ASP Basics 0 February 7th, 2005 04:02 PM
Showing Printer Dialog Box in .vbs file ntgk VBScript 0 August 6th, 2004 06:10 AM
How to change the default printer donrafeal Pro VB 6 4 April 27th, 2004 11:28 AM
Printing with default printer lletnek Beginning VB 6 1 March 4th, 2004 06:26 PM



All times are GMT -4. The time now is 10:36 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.