Link to home
Start Free TrialLog in
Avatar of manch03
manch03

asked on

Printer Script error vbs file

We have ben using this this print script for years and it has always worked.   Now we are getting this error:

Script:  c:\winnt|sysvol\sysvol\mcs.k12.mi.us\scripts\production\mmm_student_map_prn.vbs
Line 15
Char: 2
Error:  This network connection does not exist.

Code 800708ca
Source   WSHNetwor.RemoveNetworkDrive

Here is the script:

' define the variables
dim WSHNetwork
dim printers
dim c1

' create the scripting object
Set WSHNetwork = WScript.CreateObject("WScript.Network")

' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConnections()
For c1 = 0 To printers.Count - 1 Step 2
    If (Left(printers.Item(c1 + 1), 2) = "\\") then

      ' remove the printer connection
      WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

' map all the printersWSHNetwork.AddWindowsPrinterConnection
WSHNetwork.AddWindowsPrinterConnection "\\srvms1\MS_MEDIA_4240"
WSHNetwork.AddWindowsPrinterConnection "\\srvms1\MS_RM_300_HP4050_PCL"

' set a default printer
select case left(lcase(WSHNetwork.ComputerName), 4)
case "mms1", "mms2"
      WSHNetwork.SetDefaultPrinter "\\srvms1\MS_MEDIA_4240"
case "mms3"
      WSHNetwork.SetDefaultPrinter "\\srvms1\MS_RM_300_HP4050_PCL"
case else

'      if (left(lcase(WSHNetwork.ComputerName), 10) = "mmsadmin01") then
'
'      else
'
'      end if
end select

' destroy the object in memory
Set WSHNetwork = nothing

' end script file
Wscript.quit

This is making us crazy.
Avatar of Shankadude
Shankadude

Is this all the vbscript you have? The error seems to point to the removal of a network drive instead of a printer connection.
My guess would be that there is another script(part) that removes a network drive that already has been removed.
Try using the 'on error resume next'

on error resume next
For c1 = 0 To printers.Count - 1 Step 2
    If (Left(printers.Item(c1 + 1), 2) = "\\") then

     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
     If Err.Number <> 0 Then
         ' appearently the connection was already removed
         ' clear error or do something with the message
         err.clear
    End if
Next

On error go to 0 ' all errors are displayed again


Yeah I can tell you what the error is , look at what you typed here :

Code 800708ca
Source   WSHNetwor.RemoveNetworkDrive

It should be :

Code 800708ca
Source   WSHNetwork.RemoveNetworkDrive  '<-- You missed the k off at the end of WSHNetwork

Because when you created the WSHNetwork Object you assigned it / declared it as WSHNetwork not WSHNetwor

Try that and see if that helps :)
Avatar of manch03

ASKER

I see that the error was trying to remove a network drive, but I did not see anything in the script that was loading a network drive - that was confusing me.

Where should I insert this?   And do I insert  On error go to 0... line also?

Is that the whole script in its entirety ?
Have you got server 2003 R2 ?? If not just use R2 for server 2003 and you can manage and deploy printers through the new PMC ( Print Management Console )
Avatar of manch03

ASKER

Yes - the first one I posted.
Avatar of manch03

ASKER

I copied and pasted the suggestion from Shankadude under this:

' define the variables
dim WSHNetwork
dim printers
dim c1

' create the scripting object
Set WSHNetwork = WScript.CreateObject("WScript.Network")

' remove all printer connections before adding new ones
Set printers = WSHNetwork.EnumPrinterConnections()
For c1 = 0 To printers.Count - 1 Step 2
    If ((Left(printers.Item(c1 + 1), 2) = "\\") and _
      (instr(1, lcase(printers.Item(c1 + 1)), "athletics02") < 1)) then

      ' remove the printer connection
WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

and am now getting a compilation error

Line 32
Char 10

Error:  Syntax error
Code:   80003EA
Source:  Microsoft VBScript compilation error


Line 32 is this:   On error go to 0 ' all errors are displayed again
The 'on error resume next' line makes that your script doesn't break on errors anymore, but gives you the ability to handle the errors inside your script.

The 'If Err.Number <> 0 then ' part goes in front of the command you're executing like connecting to a network drive.

The 'on error go to 0' line resets the error handling again. After this your script breaks again when it encounters an error. It's up to you if you want that to happen.

But I do think that somewhere in your scripts there's something that calls the RemoveNetworkDrive.  

If you want to be sure that you use the correct variables you declared you can start the script with:
Option Explicit

After this every variable that you use has to be declared with a dim or const command. This way you can easily spot typo's in your scripts like forgetting a 'k' in network etc.

Hope this helps.
OOps, I made a type I see.. it has to be 'on error GoTo 0'.. loose the space between go and to.
Avatar of manch03

ASKER

For c1 = 0 To printers.Count - 1 Step 2
    If (Left(printers.Item(c1 + 1), 2) = "\\") then

Now it does  not like this   For statement

Error:

Line:    80
Char:    8
Error:   Invalid 'for' loop control variable
Code:    800A0410
Source:  Microsoft VBScript compilation error

In the for loop why are you checking for the 2 back slashes ie \\ ??

Why not just do this :

For c1 = 0 To printers.Count - 1 Step 2
     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

That way afterwards you just add the printer connections you want by using the select case statement as per your script :)
Avatar of manch03

ASKER

Line 80   is the "For c1 = 0
Avatar of manch03

ASKER

It still does not like this "For statement - same error

For c1 = 0 To printers.Count - 1 Step 2
     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(c1 + 1), True, True
    End if
Next

 end select

'end script file
Wscript.quit

'destroy the object in memory
'Set WSHNetwork = nothing
   End    Select
'Set printers = nothing
Instead of assigning and declaring c1 as the variable for the for loop why not just make it

dim i

For i = 0 To printers.Count - 1 Step 2
     ' remove the printer connection
     WSHNetwork.RemovePrinterConnection printers.Item(i), True, True
    End if
Next

I am not sure why you were adding 1 to the Item value so I just changed that to i but if it does not work then just put it to i + 1
Avatar of manch03

ASKER

Someone else set the scripts up and they have been working for several years and all of a sudden we were getting the errors.


But c1 is my variable and it works every place else in the script - this is the only place I am getting that error - do I just change it there and leave the others alone?
Most wise thing to do is to use variables specific for what you're doing. In this case use the variable only for deleting the printers. That way you won't mixup and the type of the variable isn't changed to an object for example by accident by another command.
Avatar of manch03

ASKER

Now I am getting this error:

  Line:   86
Char:      5
Error:     Expected statement
Code:       800A0400
Source:   Microsoft VBScript compilation error

   End if   is line 86

Instead of the script you have try this one out ( I knocked this together some time ago whilst at work as we needed one ). Let me know if this works how you expected it to.

'--------------

Dim SleepTime

Dim MSRM
Dim MSMEDIA

MSRM = "MS_RM_300_HP4050_PCL"
MSMEDIA = "MS_MEDIA_4240"

Dim AName

SleepTime=15

Set WSHNetwork = CreateObject("WScript.Network")

strComputer = WSHNetwork.ComputerName

AName = Left(lcase(strComputer,Len(strComputer)-4))

Select Case AName

    Case "mms1", "mms2"

      call AddPrinter("\\srvms1\",MSMEDIA)

    Case "mms3"

      call AddPrinter("\\srvms1\",MSRM)

End Select

Public Function AddPrinter(ByRef Server,ByRef strPrinter)

Set WshNetwork = CreateObject("WScript.Network")

rem ** This will delete existing printers **

On Error Resume Next

Set oPrinters = WshNetwork.EnumPrinterConnections

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

               WshNetwork.RemovePrinterConnection oPrinters.Item(i)

        Next

rem ** Setup new printer connection **

WshNetwork.AddWindowsPrinterConnection Server & strPrinter

rem ** Pause Script **

WScript.Sleep SleepTime*1000

rem ** Setup default printer **

WshNetwork.SetDefaultPrinter Server & strPrinter

End Function

'------------------

The strings that are assigned to MSRM and MSMEDIA should be the share names ( at least thats what they were when I used the above script so I am assuming thats what those are. If you have any issues please post back :)
ASKER CERTIFIED SOLUTION
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of manch03

ASKER

Now my users are not getting any printers, so I will give this wisesoft a try and let you know how that works out.
I gave 2 reasonable solutions and have no feedback from the asker so I would either like feedback from the asker so if there are any issues with either the script builder or the script provided that we can ( myself and the asker ) attempt to rectify any issue(s) that the asker has with the provided possible solutions.

Thanks