Solved

visual basic scripting

Posted on 2013-12-14
8
741 Views
Last Modified: 2013-12-15
I need to use a visual basic script to add a printer then change the default printer to the added printer in windows 7. I CAN'T use the usual method of going to devices and printers, and I CAN'T use group policies. PLEASE, do not suggest group policies! let's just say this HAS to be done through a visual basic script.
From googleing - Since I am NOT a scripting person - I have constructed this so far

Dim printer, UNCpath1, slash1, slash2

slash1 ="\"

slash2 ="\\"

printer = InputBox("What printer do you want to be the default? ", "Create

Default Printer")

UNCpath1 = InputBox("What server is this printer on? ", "Server")

Set printer = CreateObject("WScript.Network")
 
printer.AddWindowsPrinterConnection slash2 & UNCpath1 & slash1 & printer

printer.SetDefaultPrinter slash2 & UNCpath1 & slash1 & printer

WScript.Quit

however when the script runs I get this.
Error: Object doesn't support this property of method
and says the error is at line 13 char 1
so I'm guessing it doesn't like the slashes, however, if I take the slashes out, the script looks like this

Dim printer, UNCpath1, slash1, slash2

printer = InputBox("What printer do you want to be the default? ", "Create

Default Printer")

UNCpath1 = InputBox("What server is this printer on? ", "Server")

Set printer = CreateObject("WScript.Network")
 
printer.AddWindowsPrinterConnection  UNCpath1 & printer

printer.SetDefaultPrinter UNCpath1 & printer

WScript.Quit

and the error is
error: object doesn't support this property or method
and the line number is 9 character 1

 I can't even qualify as a noobie at scripting so I thought I would ask for some help, because I am completely guessing at what is wrong.  My guess is it doesn't want the slashes but if I don't have them, there seems to be another syntax error? ( not even sure how plausible that sounds). Or maybe it does need the slashed but I'm just adding them wrong. Also, I'm stuck on the whole slash thing because these are network printers, and usually I would add them using the UNC, for example

\\servsername\printershare

So, any ideas on how to fix my script?
0
Comment
Question by:JeffBeall
[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
  • 5
  • 3
8 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 39719232
I think you're simply a victim of copy/paste...
try this - all I've done is combine the lines as needed...
Dim printer, UNCpath1, slash1, slash2
slash1 ="\"
slash2 ="\\"
printer = InputBox("What printer do you want to be the default? ", "Create Default Printer")
UNCpath1 = InputBox("What server is this printer on? ", "Server")
Set printer = CreateObject("WScript.Network")
printer.AddWindowsPrinterConnection slash2 & UNCpath1 & slash1 & printer
printer.SetDefaultPrinter slash2 & UNCpath1 & slash1 & printer
WScript.Quit

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39719240
Whoops - nevermind - I see the problem...you're using printer twice...try this:

Dim printer, UNCpath1, slash1, slash2
slash1 ="\"
slash2 ="\\"
printername = InputBox("What printer do you want to be the default? ", "Create Default Printer")
UNCpath1 = InputBox("What server is this printer on? ", "Server")
Set printer = CreateObject("WScript.Network")
printer.AddWindowsPrinterConnection slash2 & UNCpath1 & slash1 & printername
printer.SetDefaultPrinter slash2 & UNCpath1 & slash1 & printername
WScript.Quit

Open in new window

0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 39719244
Here's a slight modification - avoids empty responses and presents the user one last chance to change their mind if the data was input incorrectly...
do
  printername = InputBox("What printer do you want to be the default? ", "Create Default Printer")
loop until printername <> ""

do
  server = InputBox("On what server is " & printername & " located?", "Server Name")
loop until server <> ""

uncpath = "\\" & server & "\" & printername

intResponse = Msgbox("Ready to add new printer, " & uncpath & "?", vbYesNo, "Confirm printer")

If intResponse = vbYes Then
  Set objNet = CreateObject("WScript.Network")
  objNet.AddWindowsPrinterConnection uncpath
  objNet.SetDefaultPrinter uncpath
Else
    Msgbox "No changes were made."
End If

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:JeffBeall
ID: 39719586
wow, thank you so much for the help. but for the shorter script, it's not setting the default printer. Did I do something wrong there?
0
 
LVL 1

Author Comment

by:JeffBeall
ID: 39719588
hang on a second, it's working, I think the problem was I was using remote desktop on a test pc, and under local resources, printers were checked.
Once I unchecked printers, it worked fine.
thanks so much again for the help.
I'm so new to scripting, I would have never have thought to put
printername =  
on that one line, especially since I didn't declare it at the DIM statement.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39719932
you DIM statement is generally an optional item.  You don't have to declare your variables upfront, unless you're script contains Option Explicit.
I rarely declare any variables in my shorter script, but it is considered good practice.
Typically anytime you declare an object that's going to have the potential to consume a lot of resources, you want to make sure you destroy it when you're done (objVariable = Nothing).
But vbscript does that upon exit/quit anyway, so again, it's mostly just good coding practice.
0
 
LVL 1

Author Closing Comment

by:JeffBeall
ID: 39719940
thank you for the help, it's working exactly how I need it to work. You were very helpful,
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39719974
Glad I could help - thanx for the grade! :^)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

OfficeMate Freezes on login or does not load after login credentials are input.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses
Course of the Month6 days, 19 hours left to enroll

622 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