Solved

VBScript Remove Printer not working

Posted on 2009-07-15
8
3,129 Views
Last Modified: 2012-05-07
I have a script for a while now, which runs when a user logs on.
It adds printers and sets the default. Users are running Windows XP.

Now I want to remove a printer, but I can't get it work.
When I try
objNetwork.RemoveWindowsPrinterConnection strUNCPrinter
I get the error
"Object doesn't support this property or method:RemoveWindowsPrinterConnection"

This happens no matter which printer I try to remove, so its not an error in my printername.
Any ideas?
Option Explicit
 
Dim objnetwork, strUNCPrinter
 
Set objNetwork = CreateObject("WScript.Network")
 
' This works
strUNCPrinter = "\\myprinterserver\W4-HPLJP3005"
objNetwork.AddWindowsPrinterConnection strUNCPrinter
objNetwork.SetDefaultPrinter strUNCPrinter
 
' This doesn't
strUNCPrinter = "\\myprinterserver\W3-HPLJ4200"
objNetwork.RemoveWindowsPrinterConnection strUNCPrinter
 
Set objNetwork = Nothing

Open in new window

0
Comment
Question by:jdhackett
[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
  • 4
  • 4
8 Comments
 
LVL 6

Expert Comment

by:ou_dober
ID: 24860969
I noticed that objNetwork.RemoveWindowsPrinterConnection should be objNetwork.RemovePrinterConnection.  Give that a try.

Option Explicit
 
Dim objnetwork, strUNCPrinter
 
Set objNetwork = CreateObject("WScript.Network")
 
strUNCPrinter = "\\myprinterserver\W4-HPLJP3005"
objNetwork.AddWindowsPrinterConnection strUNCPrinter
objNetwork.SetDefaultPrinter strUNCPrinter
 
strUNCPrinter = "\\myprinterserver\W3-HPLJ4200"
objNetwork.RemovePrinterConnection strUNCPrinter
 
Set objNetwork = Nothing

Open in new window

0
 
LVL 1

Author Comment

by:jdhackett
ID: 24861082
Thanks. RemovePrinterConnection does work.
However, it will give an error if the printer if already removed.
How can I do error handling, so that this message is suppressed?
0
 
LVL 6

Expert Comment

by:ou_dober
ID: 24861118
Add this after Line 6 to the code provided above.

On Error Resume Next
On Error GoTo 0

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jdhackett
ID: 24861414
Is there an err.escription like in normal vb?
0
 
LVL 6

Expert Comment

by:ou_dober
ID: 24861678
jdhackett,

I am not clear on what you are asking.  Are you wanting to display an error message?
0
 
LVL 1

Author Comment

by:jdhackett
ID: 24867396
Sorry, I could have put that a lot better!
I'd still like an error message displayed if there is an issue when the printers are added.
So that the user will call me and I can investigate.
However, I don't want the message when I'm removing printers.
Is there a way to do that? Thanks for your great help so far.
0
 
LVL 6

Accepted Solution

by:
ou_dober earned 500 total points
ID: 24873867
Just put the error suppression after the adding of the printer. I also added a error message that can be changed if the default printer does not install.  Is that better?  :)

ou_dober

Option Explicit
 
Dim objnetwork, strUNCPrinter
 
Set objNetwork = CreateObject("WScript.Network")
 
strUNCPrinter = "\\myprinterserver\W4-HPLJP3005"
objNetwork.AddWindowsPrinterConnection strUNCPrinter
objNetwork.SetDefaultPrinter strUNCPrinter
    If Err.Number Then
        Wscript.Echo "W4-HPLJP3005 could not be installed"
        Err.Clear
    End If
 
 
On Error Resume Next
On Error GoTo 0
 
strUNCPrinter = "\\myprinterserver\W3-HPLJ4200"
objNetwork.RemovePrinterConnection strUNCPrinter
 
Set objNetwork = Nothing

Open in new window

0
 
LVL 1

Author Comment

by:jdhackett
ID: 24876592
Great work!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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