Solved

Login Script to Add printers to Windows based machines.

Posted on 2004-04-24
11
795 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

617 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