如何用vba列出电脑上所有的打印机名称和端口?

在vba中可以用脚本对象WshNetworkEnumPrinterConnections方法枚举所有本地电脑上已经安装的打印机的名称和端口。

代码如下:

Sub QQ1722187970()
    Dim WshNetwork As Object
    Dim oPrinter As Object
    Set WshNetwork = CreateObject("WScript.Network")
    With WshNetwork
        Set oPrinter = .EnumPrinterConnections
            For i = 0 To oPrinter.Count - 1 Step 2
                Debug.Print "端口:" & oPrinter(i) & vbTab & "打印机名称:" & oPrinter(i + 1)
            Next i
    End With
End Sub

 

       

发表评论