0

我有一些代码列出了所有活动的打印机(但在 msgboxes 而不是列表中这样做)。我对该功能没有任何问题。下面是代码:

    If Printers.Count > 0 Then
    strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf
    For Each prtLoop In Application.Printers
        With prtLoop
           MsgBox .DeviceName                
      End With
    Next prtLoop
    Else
    MsgBox "No printers are installed."
    End If

虽然它完成了我本质上需要它做的事情,但我希望它仅在与代码中设置的打印机名称的一部分匹配的打印机时才弹出。例如,如果我有 3 台打印机:

Printer ABC 1
Printer ZZ5 2 (copy)
Printer 123

我希望有一个 If Then 语句可能在

    For Each prtLoop In Application.Printers

上面代码的一部分表示如果设备名称是 *ABC 那么它只会弹出“Printer ABC 1”而不是全部 3。

我希望这是有道理的。在此先感谢您的帮助。-弗拉基米尔

4

1 回答 1

1

也许这就是你想做的?
If Printers.Count > 0 Then strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf For Each prtLoop In Application.Printers With prtLoop If InStr(DeviceName, "ABC") Then MsgBox .DeviceName
End If
End With Next prtLoop Else MsgBox "No printers are installed." End If

编辑:Access 不喜欢 If then 语句,因为它是一行,错误是出现“End If without Block If”错误,我将 MsgBox .DeviceName 移动到它自己的行并且它可以工作。

于 2017-01-23T14:34:46.850 回答