码迷,mamicode.com
首页 > 编程语言 > 详细

VBA Find Printers

时间:2016-07-11 12:13:43      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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:\\.\root\default:StdRegProv")
    
    objRegistry.EnumValues &H80000001, "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts", arrSubkeys
    FindPrinter = False
    length = Len(strPrinter)
    For Each subkey In arrSubkeys
        objRegistry.GetStringValue &H80000001, "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\", 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

 

VBA Find Printers

标签:

原文地址:http://www.cnblogs.com/mymeng_feng/p/4359270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!