Setting default printer to LPT1 with vbscript

Hello,

I was wondering if anyone knows how to set a default printer to the LPT port without using the name of the printer by using vbscript.


Thanks!

uucutechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
I think you will atleast need the printers share name.
0
uucutechAuthor Commented:
I don't need to add the printer.....it's already been installed and setup.  A number of network printers are added using the folowwing vbscript at logon.  I just need to set the default printer to LPT1 without using the printer name because its possible they could differ from workstation to workstation.

===========================================================

ON ERROR RESUME NEXT

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
DomainString = "uofucu"
UserString = WSHNetwork.UserName

Set UserObj = GetObject("WinNT://domain/" & UserString)

WshShell.RegWrite "HKEY_CURRENT_USER\Software\Symitar\SFW\2.0\Browser Options\Startup Page", "http://10.1.1.23/cuweb/home.htm"
WshShell.RegWrite "HKEY_CURRENT_USER\Software\Symitar\SFW\2.0\Browser Options\Information Page", "http://10.1.1.23/cuweb/home.htm"

WshShell.RegWrite "HKEY_LOCAL_MACHINE\Software\Symitar\SFW\2.0\Browser Options\Startup Page", "http://10.1.1.23/cuweb/home.htm"
WshShell.RegWrite "HKEY_LOCAL_MACHINE\Software\Symitar\SFW\2.0\Browser Options\Information Page", "http://10.1.1.23/cuweb/home.htm"

WshShell.RegWrite "HKEY_USERS\.DEFAULT\Software\Symitar\SFW\2.0\Browser Options\Startup Page", "http://10.1.1.23/cuweb/home.htm"
WshShell.RegWrite "HKEY_USERS\.DEFAULT\Software\Symitar\SFW\2.0\Browser Options\Information Page", "http://10.1.1.23/cuweb/home.htm"

'Synchronizes the time with Server our NTP Server
'WSHShell.Run "NET TIME \\uucutestdc /set /y"

'Remove Network drive
'WSHNetwork.RemoveNetworkDrive "i:"
'WSHNetwork.RemoveNetworkDrive "L:"
'WSHNetwork.RemoveNetworkDrive "U:"


wscript.sleep 300

'Maps drives needed by all
 WSHNetwork.MapNetworkDrive "P:", "\\server\share",True

 WSHNetwork.MapNetworkDrive "U:", "\\server\share",True

'WSHNetwork.MapNetworkDrive "X:", "\\server\share",True


'Now check for group memberships and map appropriate drives
For Each GroupObj In UserObj.Groups

Select Case GroupObj.Name
'Check for group memberships and take needed action
    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True
   
    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True

    Case "group"
        WSHNetwork.MapNetworkDrive "G:", "\\server\share",True


End Select

Next


'Now check for group memberships and map appropriate printers
For Each GroupObj In UserObj.Groups

Select Case GroupObj.Name
'Check for group memberships and take needed action
    Case "group"
          WSHNetwork.RemoveWindowsPrinterConnection "\\Server\printer"
    Case "group"
            WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
          WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
            Wscript.Sleep 300
          WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
               WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
               WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
               WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
               WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
             WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
               Wscript.Sleep 300
             WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"            
             WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
             Wscript.Sleep 300
             WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
            WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"
    Case "group"
            WSHNetwork.AddWindowsPrinterConnection "\\Server\printer"

============================================================

I repeat,  the printer is already added......I just need to set the "Default Printer" to LPT1 wihtout using the actual name of the printer.

Thanks!



      
      
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

uucutechAuthor Commented:
It's not a shared printer......its local using the LPT port
0
EDDYKTCommented:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * from Win32_Printer where portname ='LPT1:'")
For Each objPrinter In colInstalledPrinters
    objPrinter.SetDefaultPrinter
Next
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
uucutechAuthor Commented:
Can i just cut and paste that slice of code anywhere into the script and it should work?  Also will i need to change anything to make it proprietary to our environment.  Sorry I'm pretty new to vbscript  :S  


Thanks for your help
0
EDDYKTCommented:
You should just cut and paste and it should works.

Howver, the code may not work if the user who runs the script has no permission to set the default printer.
0
uucutechAuthor Commented:
i put it at the end of my script, and it doesnt seem to want to work.....
0
EDDYKTCommented:
Try to isloate the problem, Just copy to new vbs and try.
It works for me on XP.
0
uucutechAuthor Commented:
I copied the code into a new vbs file and ran it on a machine that has 1 network printer and 1 local printer.  This machine is running Win Xp pro SP2.......still doesn't work.
0
EDDYKTCommented:

' See any popup message?


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * from Win32_Printer where portname ='LPT1:'")
For Each objPrinter In colInstalledPrinters
wscript.echo objPrinter.name
    objPrinter.SetDefaultPrinter
Next
0
Louis01Commented:
To detect which printer is on lpt1:
'-----------------------------
  Dim pr As Printer
   
  For Each pr In Printers
    If InStr(LCase(pr.Port), "lpt1") > 0 Then
      SetDefaultPrinter pr.devicename '(Not included - see below)
    End If
  Next
'-----------------------------
To set the default printer:
See http://vbnet.mvps.org/index.html?code/system/defprinternt.htm
0
uucutechAuthor Commented:
when i run that bit of code you posted i get an error stating:

--------------------------------------------
Line:   1
Char:   10
Error:   Expected End of Statement
Code:   800A0401
Source:   MS VBscript comp error
---------------------------------------------

I tried the one posted at that link as well and this is the error that i get:

---------------------------------------------
Line:    14
Char:    30
Error:    Expected '='
Code:    800A03F3
Source:    MS VBscript comp error
---------------------------------------------

So I'm still at a stand still

0
uucutechAuthor Commented:
that last comment was directed at Louis01
0
Louis01Commented:
You will get an error because you need to write the SetDefaultPrinter routine based on information you can find in

http://vbnet.mvps.org/index.html?code/system/defprinternt.htm

Or use

Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\\research\library1"
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath

Or use info at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthaddprinterconnection.asp
0
EDDYKTCommented:
Do you run my post code and get an error?

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * from Win32_Printer where portname ='LPT1:'")
For Each objPrinter In colInstalledPrinters
wscript.echo objPrinter.name
    objPrinter.SetDefaultPrinter
Next


You
0
uucutechAuthor Commented:
Louis01,

That seems a little more extensive than what i need.  If there is a way to use the "WshNetwork.SetDefaultPritnter"  command to just use LPT1  that sould be ideal.  I tried using this at the end of my script:

WshNetwork.SetDefaultPritnter "LPT1"  

but it still doesn't work.

EDDYKT:

no i do not get an error when using that section of code that you posted

Thanks again for your help.  Hopefully we can get these points awarded soon!

Dustin

0
EDDYKTCommented:
On more question

does this line from the above example got excute?


wscript.echo objPrinter.name

if not you may not have any printer attached to your lpt1:

Use this to print out all printer attached to lpt1:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
For Each objPrinter In colInstalledPrinters
wscript.echo objPrinter.portname
    'objPrinter.SetDefaultPrinter
Next
0
uucutechAuthor Commented:
Ok,

If i take this line out of the script: wscript.echo objPrinter.name
Will it still run correctly?  It just won't print the name of the printer that is attached to LPT1 right?


it seemed to work......i was doing my testing on a machine that had the driver installed for the printer but the actual physical printer wasnt connected.  I thought it would work like that but apparently i was wrong :O  

Thanks,

DUstin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.