Beginner VB

Moderators: None (Apply to moderate this forum)
Number of threads: 1244
Number of posts: 2990

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
VBscript install printer driver Posted by flatleyld on 4 Jan 2011 at 3:55 PM
I couldn't find a VBscript forum so I hope this is ok.

I am making a script that is supposed to change the port of the printer to FILE: (that's not my problem). This script is also supposed to go through the list of installed printer drivers and if there is no HP Laserjet 4050 PCL6 installed then install the driver. If it is installed then just change the driver of a specific printer to the Laserjet 4050 PCL6. The problem I have is that I get an error 87 every time I run it. I know it has something to do with the printer install. If I comment that out the whole script runs fine. Any ideas?

Here is the code:
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID='ProDOC Printer'")
Set colPrintdrivers = objWMIService.ExecQuery _
	("Select * from Win32_PrinterDriver ")
	
flag = 0
'Look for laserjet driver in installed list
For Each objPrint in colPrintdrivers
	If objPrint.Name <> "HP Laserjet 4050 PCL6" Then
		flag = 1
	End if
Next
For Each objPrinter in colPrinters
	'set port to FILE:
    objPrinter.PortName = "FILE:"
    'install the printer driver if it wasn't found earlier
    If flag = 1 then
    	Set objDriver = objWMIService.Get("Win32_PrinterDriver")
		objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
		objDriver.Name = "HP Laserjet 4050 PCL6"
		objDriver.SupportedPlatform = "Windows NT x86"
		objDriver.Version = "3"
		errResult = objDriver.AddPrinterDriver(objDriver)
	 End If
	'if the driver is installed, change the driver of the selected printer
    If flag = 0 Then
    	objPrinter.driverName = "HP Laserjet 4050 Series PCL6"
    End If
    objPrinter.Put_
Next
'display results
WScript.Echo ErrResult
Report
Re: VBscript install printer driver Posted by flatleyld on 5 Jan 2011 at 5:02 PM
Here's an update. I found out after tinkering that the driver name of my HP Laserjet 4050 PCL6 is wrong. I've tested this script with the laserjet 4 driver and that works. Does anyone know the proper name for the laserjet 4050 PCL6?
Report
Re: VBscript install printer driver Posted by flatleyld on 5 Jan 2011 at 5:09 PM
Looks like I solved my own problem. Here is my finished working code:
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where DeviceID='ProDOC Printer'")
Set colPrintdrivers = objWMIService.ExecQuery _
	("Select * from Win32_PrinterDriver ")
	
flag = 0
'Look for laserjet driver in installed list
For Each objPrint in colPrintdrivers
	If objPrint.Name <> "HP Laserjet 4050 Series PCL6" Then
		flag = 1
	End if
Next
For Each objPrinter in colPrinters
	'set port to FILE:
    objPrinter.PortName = "FILE:"
    'install the printer driver if it wasn't found earlier
    If flag = 1 then
    	Set objDriver = objWMIService.Get("Win32_PrinterDriver")
		objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
		objDriver.Name = "HP Laserjet 4050 Series PCL6"
		objDriver.SupportedPlatform = "Windows NT x86"
		objDriver.Version = "3"
		errResult = objDriver.AddPrinterDriver(objDriver)
	 End If
    objPrinter.driverName = "HP Laserjet 4050 Series PCL6"
    objPrinter.Put_
Next
'display results
WScript.Echo ErrResult



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.