Hi,

I'm trying to do a login vbs script that deletes a printer if it exists on
the machine. I can delete the printer if it exists OK, so that the first
time a user logs on, all is well. However, the next time the script runs, I
get an error stating:

"This network connection does not exist"

What I want to do is put an if exists statement around the deletion, so that
if it isn't there, the script doesn't try to delete it.

The script so far looks like:

strPrinter="\\DC2\Development"
Set WshNetwork = CreateObject("WScript.Network")
Set WshShell = CreateObject("WScript.Shell")
WshNetwork.AddWindowsPrinterConnection strPrinter
WshNetwork.SetDefaultPrinter strPrinter
MsgBox strPrinter & " Printer connected and set as the default printer"

Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\\DC2\Engineering"

if Exists (PrinterPath) Then
WshNetwork.RemovePrinterConnection PrinterPath, true, true
MsgBox PrinterPath & " Printer Removed"
MsgBOx "Default printer is now " & strPrinter
End If

This errors with a Type Mismatch: 'Exists'

What am I missing her? Can anyone help?

Thanks

John

Re: Disconnect Printer If it exists by Torgeir

Torgeir
Tue Jan 25 12:06:34 CST 2005

John wrote:

> Hi,
>
> I'm trying to do a login vbs script that deletes a printer if it exists on
> the machine. I can delete the printer if it exists OK, so that the first
> time a user logs on, all is well. However, the next time the script runs, I
> get an error stating:
>
> "This network connection does not exist"
>
> What I want to do is put an if exists statement around the deletion, so that
> if it isn't there, the script doesn't try to delete it.
>
> The script so far looks like:
>
> strPrinter="\\DC2\Development"
> Set WshNetwork = CreateObject("WScript.Network")
> Set WshShell = CreateObject("WScript.Shell")
> WshNetwork.AddWindowsPrinterConnection strPrinter
> WshNetwork.SetDefaultPrinter strPrinter
> MsgBox strPrinter & " Printer connected and set as the default printer"
>
> Set WshNetwork = WScript.CreateObject("WScript.Network")
> PrinterPath = "\\DC2\Engineering"
>
> if Exists (PrinterPath) Then
> WshNetwork.RemovePrinterConnection PrinterPath, true, true
> MsgBox PrinterPath & " Printer Removed"
> MsgBOx "Default printer is now " & strPrinter
> End If
>
> This errors with a Type Mismatch: 'Exists'
>
> What am I missing her? Can anyone help?
Hi


You could replace:

if Exists (PrinterPath) Then
WshNetwork.RemovePrinterConnection PrinterPath, true, true
MsgBox PrinterPath & " Printer Removed"
MsgBOx "Default printer is now " & strPrinter
End If


with:

On Error Resume Next
WshNetwork.RemovePrinterConnection PrinterPath, true, true
If Err.Number = 0 Then
MsgBox PrinterPath & " Printer Removed"
MsgBox "Default printer is now " & strPrinter
End If
On Error Goto 0



--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Disconnect Printer If it exists by John

John
Wed Jan 26 05:02:00 CST 2005

Hi Torgeir.

Thanks very much for that. It worked a treat.

Regards,

John

"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:%23QC7TjwAFHA.2560@TK2MSFTNGP10.phx.gbl...
> John wrote:
>
>> Hi,
>>
>> I'm trying to do a login vbs script that deletes a printer if it exists
>> on the machine. I can delete the printer if it exists OK, so that the
>> first time a user logs on, all is well. However, the next time the script
>> runs, I get an error stating:
>>
>> "This network connection does not exist"
>>
>> What I want to do is put an if exists statement around the deletion, so
>> that if it isn't there, the script doesn't try to delete it.
>>
>> The script so far looks like:
>>
>> strPrinter="\\DC2\Development"
>> Set WshNetwork = CreateObject("WScript.Network")
>> Set WshShell = CreateObject("WScript.Shell")
>> WshNetwork.AddWindowsPrinterConnection strPrinter
>> WshNetwork.SetDefaultPrinter strPrinter
>> MsgBox strPrinter & " Printer connected and set as the default
>> printer"
>>
>> Set WshNetwork = WScript.CreateObject("WScript.Network")
>> PrinterPath = "\\DC2\Engineering"
>>
>> if Exists (PrinterPath) Then
>> WshNetwork.RemovePrinterConnection PrinterPath, true, true
>> MsgBox PrinterPath & " Printer Removed"
>> MsgBOx "Default printer is now " & strPrinter
>> End If
>>
>> This errors with a Type Mismatch: 'Exists'
>>
>> What am I missing her? Can anyone help?
> Hi
>
>
> You could replace:
>
> if Exists (PrinterPath) Then
> WshNetwork.RemovePrinterConnection PrinterPath, true, true
> MsgBox PrinterPath & " Printer Removed"
> MsgBOx "Default printer is now " & strPrinter
> End If
>
>
> with:
>
> On Error Resume Next
> WshNetwork.RemovePrinterConnection PrinterPath, true, true
> If Err.Number = 0 Then
> MsgBox PrinterPath & " Printer Removed"
> MsgBox "Default printer is now " & strPrinter
> End If
> On Error Goto 0
>
>
>
> --
> torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of
> the 1328 page Scripting Guide:
> http://www.microsoft.com/technet/scriptcenter/default.mspx