Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > 综合其它

【译文】如何检索已安装打印机的列表

时 间:2013-12-31 08:35:39
作 者:周芳   ID:24526  城市:上海
摘 要:如何检索已安装打印机的列表
正 文:

来自:微软    翻译:周芳

【译文 如何检索已安装打印机的列表


      使用打印机属性应用程序对象来返回打印机集。下面的程序演示了如何使用For Each…Next 语句来通过每个打印机对象遍历打印机。消息框来显示每个安装的打印机的信息。


VBA代码:
Sub ShowPrinters()
    Dim strCount As String
    Dim strMsg As String
    Dim prtLoop As Printer
    
    On Error GoTo ShowPrinters_Err
 
    If Printers.Count > 0 Then
        ' 获取已安装打印机数目.
        strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf
    
        ' 列举出打印系统的属性.
        For Each prtLoop In Application.Printers
            With prtLoop
                strMsg = strMsg _
                    & "Device name: " & .DeviceName & vbCrLf _
                    & "Driver name: " & .DriverName & vbCrLf _
                    & "Port: " & .Port & vbCrLf & vbCrLf
            End With
        Next prtLoop
    
    Else
        strMsg = "没有安装打印机."
    End If
    
    ' 显示打印机信息.
    MsgBox Prompt:=strMsg, Buttons:=vbOKOnly, Title:="Installed Printers"
    
ShowPrinters_End:
    Exit Sub
    
ShowPrinters_Err:
    MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
        Title:="Error Number " & Err.Number & " Occurred"
    Resume ShowPrinters_End
    
End Sub



【原文】How to: Retrieve a List of Installed Printers

       You use the Printers property of the Application object to return the Printers collection. The following procedure illustrates how to enumerate through each Printer object in the Printers collection by using a For Each…Next statement. A message box is displayed with information about each installed printer.

VBA:
Sub ShowPrinters()
    Dim strCount As String
    Dim strMsg As String
    Dim prtLoop As Printer
    
    On Error GoTo ShowPrinters_Err
 
    If Printers.Count > 0 Then
        ' Get count of installed printers.
        strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf
    
        ' Enumerate printer system properties.
        For Each prtLoop In Application.Printers
            With prtLoop
                strMsg = strMsg _
                    & "Device name: " & .DeviceName & vbCrLf _
                    & "Driver name: " & .DriverName & vbCrLf _
                    & "Port: " & .Port & vbCrLf & vbCrLf
            End With
        Next prtLoop
    
    Else
        strMsg = "No printers are installed."
    End If
    
    ' Display printer information.
    MsgBox Prompt:=strMsg, Buttons:=vbOKOnly, Title:="Installed Printers"
    
ShowPrinters_End:
    Exit Sub
    
ShowPrinters_Err:
    MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
        Title:="Error Number " & Err.Number & " Occurred"
    Resume ShowPrinters_End
    
End Sub



Access软件网官方交流QQ群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助