Solved

Login Script to Add printers to Windows based machines.

Posted on 2004-04-24
11
792 Views
Last Modified: 2012-08-13
Can I anyone help me with code. This is my login script on Windows2000 server for setting up of printers. When a user is presented with a box "Where are you" and they type " Guest Area". Only the last printer on that list is added to their printers and rest printers are ignored. I'm new to VB code and can't figure out what am I doing wrong. Also is there anyway that user input is not Case Sensitive. I thought VB code is not case sensitive but guess I was wrong.

"
option explicit
dim oNetwork, sPrintPath, sLocate
Set oNetwork = CreateObject("WScript.Network")
sLocate=_
InputBox("Where are you? Type 'Production Room' , 'Library' , or 'Guest Area'.")
Select Case sLocate
Case "Production Room"
      sPrintPath = "\\nexsxxxx\nexp4001"
      sPrintPath = "\\nexsxxxx\XeroxDC340
      sPrintPath = "\\nexsxxxx\Xerox_color"
Case "Library"
      sPrintPath = "\\nexsxxxx\nexp4003"
      sPrintPath = "\\nexsxxxx\Xerox\DC432"
      sPrintPath = "\\nexsxxxx\Xerox_color"
Case "Guest Area"
      sPrintPath = "\\nexsxxxx\nexp4004"
      sPrintPath = "\\nexsxxxx\Xerox\DC432"
      sPrintPath = "\\nexsxxxx\Xerox_color"
Wscript.Echo " This is not a valid choice." :Wscrip.Quit
End Select
oNetwork.AddWindowsPrinterConnection SprintPath
oNetwork.SetDefaultPrinter sPrintPath
Set oNetwork = vbEmpty
Set sPrintPath = vbEmpty
"
0
Comment
Question by:fiarik
  • 6
  • 4
11 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 10908552
First problem I see is you're missing a CASE ELSE clause - what if the don't type anything you've provided, or mistype it?
Secondly, you're defining sPrintPath three times...you need either 3 variables or make this one an array...

Case "Production Room"
     sPrintPath(0) = "\\nexsxxxx\nexp4001"
     sPrintPath(1) = "\\nexsxxxx\XeroxDC340
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
0
 
LVL 3

Expert Comment

by:Level10Access
ID: 10910226
Ok. Heres the all correct code :-)

option explicit
dim oNetwork, sPrintPath(2), sLocate
dim done as boolean
done = 0
Set oNetwork = CreateObject("WScript.Network")

sLocate=_
InputBox("Where are you? Type 'Production Room' , 'Library' , or 'Guest Area'.")
do until done = 1
Select Case sLocate
Case "Production Room"
     sPrintPath(0) = "\\nexsxxxx\nexp4001"
     sPrintPath(1) = "\\nexsxxxx\XeroxDC340
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Library"
     sPrintPath(1) = "\\nexsxxxx\nexp4003"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Guest Area"
     sPrintPath(1) = "\\nexsxxxx\nexp4004"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case else
     Wscript.Echo " This is not a valid choice."
     wscript.quit
End Select
loop
dim x
for x = 0 to 2
oNetwork.AddWindowsPrinterConnection SprintPath(x)
oNetwork.SetDefaultPrinter sPrintPath(x)
next
Set oNetwork = vbEmpty
Set sPrintPath = vbEmpty
0
 
LVL 3

Expert Comment

by:Level10Access
ID: 10910231
oops correction!!!

Ok. Heres the all correct code :-)

option explicit
dim oNetwork, sPrintPath(2), sLocate
dim done as boolean
done = 0
Set oNetwork = CreateObject("WScript.Network")
do until done = 1
sLocate=_
InputBox("Where are you? Type 'Production Room' , 'Library' , or 'Guest Area'.")
Select Case sLocate
Case "Production Room"
     sPrintPath(0) = "\\nexsxxxx\nexp4001"
     sPrintPath(1) = "\\nexsxxxx\XeroxDC340
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Library"
     sPrintPath(1) = "\\nexsxxxx\nexp4003"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Guest Area"
     sPrintPath(1) = "\\nexsxxxx\nexp4004"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case else
     Wscript.Echo " This is not a valid choice."
End Select
loop
dim x
for x = 0 to 2
oNetwork.AddWindowsPrinterConnection SprintPath(x)
oNetwork.SetDefaultPrinter sPrintPath(x)
next
Set oNetwork = vbEmpty
Set sPrintPath = vbEmpty
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:fiarik
ID: 10918335
Level 10 Access.. Thanks for you the correct code but I'm sorry to say it doesn't work. It first has run time error. The first error is popped in line 3 Character 10 ( Expected End of Statmenet ). Remember I'm using this code as a VBScript as login script which is called from .BAT file. If I correct the this error and run the script it pops up error when I select the location and error is as follow. { Line: 23 , Char 6 , Error Subscript out of range:'[number: 3]', code: 800A0009, Source: Microsoft VBScript runtime error }. If this error is corrected by deleting the line it pops up with another run time error { Line 30, Char 1 , Error: The printer name is invalid, Code: 80070709 , Source ( null ) }
0
 
LVL 3

Expert Comment

by:Level10Access
ID: 10922574
option explicit
dim oNetwork, sPrintPath(2), sLocate, done, x
done = 0
Set oNetwork = CreateObject("WScript.Network")
do until done = 1
sLocate=_
InputBox("Where are you? Type 'Production Room' , 'Library' , or 'Guest Area'.")
Select Case sLocate
Case "Production Room"
     sPrintPath(0) = "\\nexsxxxx\nexp4001"
     sPrintPath(1) = "\\nexsxxxx\XeroxDC340
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Library"
     sPrintPath(1) = "\\nexsxxxx\nexp4003"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Guest Area"
     sPrintPath(1) = "\\nexsxxxx\nexp4004"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case else
     Wscript.Echo " This is not a valid choice."
End Select
loop

for x = 0 to 2
oNetwork.AddWindowsPrinterConnection SprintPath(x)
oNetwork.SetDefaultPrinter sPrintPath(x)
next
Set oNetwork = vbEmpty
Set sPrintPath = vbEmpty

yea that should work! sorry about all this, I cant debug it because your the one that has all the printers and such.  try this though. Also, deleting the lines wont help :-)
0
 
LVL 3

Expert Comment

by:Level10Access
ID: 10922576
option explicit
dim oNetwork, sPrintPath(2), sLocate, done, x
done = 0
Set oNetwork = CreateObject("WScript.Network")
do until done = 1
sLocate=_
InputBox("Where are you? Type 'Production Room' , 'Library' , or 'Guest Area'.")
Select Case sLocate
Case "Production Room"
     sPrintPath(0) = "\\nexsxxxx\nexp4001"
     sPrintPath(1) = "\\nexsxxxx\XeroxDC340
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Library"
     sPrintPath(1) = "\\nexsxxxx\nexp4003"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Guest Area"
     sPrintPath(1) = "\\nexsxxxx\nexp4004"
     sPrintPath(2) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(3) = "\\nexsxxxx\Xerox_color"
     done = 1
Case else
     Wscript.Echo " This is not a valid choice."
End Select
loop

for x = 0 to 2
oNetwork.AddWindowsPrinterConnection SprintPath(x)
oNetwork.SetDefaultPrinter sPrintPath(x)
next
Set oNetwork = vbEmpty
Set sPrintPath = vbEmpty

yea that should work! sorry about all this, I cant debug it because your the one that has all the printers and such.  try this though. Also, deleting the lines wont help :-)
0
 

Author Comment

by:fiarik
ID: 10928414
Error is still there, I'm getting this error { Line: 22 , Char 6 , Error Subscript out of range:'[number: 3]', code: 800A0009, Source: Microsoft VBScript runtime error }.

This is the same error I was getting yesterday and then if some how I correct this error I start getting errors on
oNetwork.AddWindowsPrinterConnection SprintPath(x)
oNetwork.SetDefaultPrinter sPrintPath(x)

Can anyone help
0
 
LVL 3

Expert Comment

by:Level10Access
ID: 10930567
DOH! Sorry, here we go.

option explicit
dim oNetwork, sPrintPath(2), sLocate, done, x
done = 0
Set oNetwork = CreateObject("WScript.Network")
do until done = 1
sLocate=_
InputBox("Where are you? Type 'Production Room' , 'Library' , or 'Guest Area'.")
Select Case sLocate
Case "Production Room"
     sPrintPath(0) = "\\nexsxxxx\nexp4001"
     sPrintPath(1) = "\\nexsxxxx\XeroxDC340
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Library"
     sPrintPath(0) = "\\nexsxxxx\nexp4003"
     sPrintPath(1) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     done = 1
Case "Guest Area"
     sPrintPath(0) = "\\nexsxxxx\nexp4004"
     sPrintPath(1) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     done = 1
Case else
     Wscript.Echo " This is not a valid choice."
End Select
loop

for x = 0 to 2
oNetwork.AddWindowsPrinterConnection SprintPath(x)
oNetwork.SetDefaultPrinter sPrintPath(x)
next
Set oNetwork = vbEmpty
Set sPrintPath = vbEmpty
0
 

Author Comment

by:fiarik
ID: 10930694
Thanks that fixes the problem but now I run into this error { Line 30, Char 1 , Error: The printer name is invalid, Code: 80070709 , Source ( null ) }. I don't see any printer name mentioned on line 30, anyway to fix this.

0
 
LVL 3

Accepted Solution

by:
Level10Access earned 150 total points
ID: 10934040
OK are you ready? This will work PERFECT if those printer names exist. Here you go.
also, I realized this question is only worth 50 points. I really put alot of hard work into this, so do you think you could maybe bump up the points a little? :-) Thanks mate. Enjoy!

option explicit
on error resume next
dim oNetwork, sPrintPath(2), sLocate, done, x, location
done = 0
Set oNetwork = CreateObject("WScript.Network")
do until done = 1
sLocate = InputBox("Where are you? Type 'Production Room' , 'Library' , 'Guest Area'.")
Select Case ucase(sLocate)
Case "PRODUCTION ROOM"
     sPrintPath(0) = "\\nexsxxxx\nexp4001"
     sPrintPath(1) = "\\nexsxxxx\XeroxDC340"
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     location = "Production Room"
     done = 1

Case "LIBRARY"
     sPrintPath(0) = "\\nexsxxxx\nexp4003"
     sPrintPath(1) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     location = "Library"
     done = 1
Case "GUEST AREA"
     sPrintPath(0) = "\\nexsxxxx\nexp4004"
     sPrintPath(1) = "\\nexsxxxx\Xerox\DC432"
     sPrintPath(2) = "\\nexsxxxx\Xerox_color"
     location = "Guest Area"
     done = 1
Case else
     Wscript.Echo " This is not a valid choice."
End Select
loop

for x = 0 to 2
oNetwork.AddWindowsPrinterConnection SprintPath(x)
oNetwork.SetDefaultPrinter sPrintPath(x)
if err.number <> 0 then
msgbox "Error: " & err.description & chr(13)_
 & "Please contact your network administrator for further assistance."
end if
next
msgbox "Welcome to the " & location & "!"

Set oNetwork = vbEmpty
Set sPrintPath = vbEmpty
0
 

Author Comment

by:fiarik
ID: 10938849
Thanks. I increased the points from 50 to 150 :-)...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 104
VB6 - Convert HH:MM into Decimal 8 62
VBA to find and replace multiline text from VBA modules 8 64
adding "ungroup sheets" to existing vbs code 5 30
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question