[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Setting default printer to LPT1 with vbscript

Posted on 2004-11-22
19
Medium Priority
?
1,662 Views
Last Modified: 2012-06-27
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!

0
Comment
Question by:uucutech
  • 9
  • 7
  • 2
  • +1
19 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 12646851
I think you will atleast need the printers share name.
0
 

Author Comment

by:uucutech
ID: 12646909
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
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!

 

Author Comment

by:uucutech
ID: 12646916
It's not a shared printer......its local using the LPT port
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 2000 total points
ID: 12647211
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
 

Author Comment

by:uucutech
ID: 12647254
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12647412
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
 

Author Comment

by:uucutech
ID: 12648501
i put it at the end of my script, and it doesnt seem to want to work.....
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12654064
Try to isloate the problem, Just copy to new vbs and try.
It works for me on XP.
0
 

Author Comment

by:uucutech
ID: 12656325
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12656382

' 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
 
LVL 11

Expert Comment

by:Louis01
ID: 12664235
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
 

Author Comment

by:uucutech
ID: 12669945
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
 

Author Comment

by:uucutech
ID: 12669955
that last comment was directed at Louis01
0
 
LVL 11

Expert Comment

by:Louis01
ID: 12672070
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12674097
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
 

Author Comment

by:uucutech
ID: 12788601
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12791557
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
 

Author Comment

by:uucutech
ID: 12793844
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 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