Option Explicit
'On Error Resume Next
Dim oWshShell, objWMIService
Set oWshShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Function PrinterExists(PrinterName)
Dim colPrinters, objPrinter
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where DeviceID = '" & PrinterName & "'")
PrinterExists = False
For Each objPrinter in colPrinters
PrinterExists = True
Next
End Function
Sub RenamePrinter(NameBefore, NameAfter)
Dim colPrinters, objPrinter
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where DeviceID = '" & NameBefore & "'")
For Each objPrinter in colPrinters
objPrinter.RenamePrinter(NameAfter)
Next
End Sub
Function InstallPrinter(PrinterName)
If Not PrinterExists(PrinterName) Then
Installing = oWshShell.Run("rundll32 printui.dll PrintUIEntry /if /f %windir%\inf\ntprint.inf /r ""file:"" /m """ & PrinterName & """")
End If
Dim FailCount
FailCount = 0
Do While Not PrinterExists(PrinterName) And FailCount < 5
WScript.Sleep 1000
FailCount = FailCount + 1
Loop
InstallPrinter = PrinterExists(PrinterName)
End Function
Dim Installing
If Not PrinterExists("PostScript Printer") Then
'XP makes this apple printer available
If InstallPrinter("Apple Color LW 12/660 PS") Then
RenamePrinter "Apple Color LW 12/660 PS", "PostScript Printer"
Else
'Don't know of a vista one yet
End If
End If
以下代码对我有用。不完美,但它有效。