Public Function FindPrinter(strPrinterName As String) As Boolean Dim pa As String Dim length As Integer Dim objRegistry As Object Dim arrSubkeys As Variant Dim subkey As Variant Dim kk As Variant On Error Resume Next Set objRegistry = GetObject("winmgmts:\. ootdefault:StdRegProv") objRegistry.EnumValues &H80000001, "SoftwareMicrosoftWindows NTCurrentVersionPrinterPorts", arrSubkeys FindPrinter = False length = Len(strPrinter) For Each subkey In arrSubkeys objRegistry.GetStringValue &H80000001, "SoftwareMicrosoftWindows NTCurrentVersionPrinterPorts", subkey, kk pa = subkey & " on " & Mid(kk, InStr(kk, ",") + 1, InStr(kk, ":,") - InStr(kk, ",")) If Left(pa, length) = strPrinterName Then FindPrinter = True Application.ActivePrinter = pa Exit For End If Next If FindPrinter = False Then MsgBox "没有找到打印机。" End If End Function