MS ACCESS:通过VBA更改默认打印机

时间:2015-05-27 14:18:56

标签: vba ms-access

我有一个小应用程序,将默认打印机更改为Ithica票据打印机,然后打印一些信息并重置为默认打印机。该程序将在三个不同的工作站使用,每个工作站都有自己的票据打印机。

以下是我目前的代码尝试,它是炸弹。有什么问题?

"application.Printer = application.Printers(tempTicket)"

使用"设备和打印机"

中的打印机名称
   Private Sub Printfoundcardbtn_Click()
   Dim stEnviron As String
   Dim tempTicket As String
   '----- get the Computer Name
       stEnviron = Environ$("COMPUTERNAME")
'-------------Set ticket printer associated with "COMPUTERNAME"
        Select Case [stEnviron]
    Case "MyComputer"
        tempTicket = "Ithaca USB Printer"
    Case "SSP-REG1"
        tempTicket = "Ithaca USB Printer"
    Case "SSPREGA2"
        tempTicket = "ITherm610"

 End Select


    '-----------------------------------------------------------------
    '-------Set to ticket printer then back to Default---------------------------
    '------------------------------------------------------------------

     Dim prt As Printer
     ' Get current default printer
     Set prt = Application.Printer
     ' Set default printer
     application.Printer = Application.Printers(tempTicket)
     ' Print something, e.g.
     DoCmd.OpenForm "couponPrintFForm"
     '
     DoCmd.PrintOut acPages = 1
          ' Restore original printer
     Set Application.Printer = prt
     DoCmd.Close acForm, "couponPrintFForm", acSave = yes


End Sub

1 个答案:

答案 0 :(得分:1)

Dim prn As Printer
Dim printerFound As Boolean: printerFound = False

For Each prn In Application.Printers
    If prn.DeviceName = tempTicket Then
        Application.Printer = prn
        printerFound = True
        Exit For
    End If
Next

If Not printerFound Then
    ' Handle Printer Not Found Error
End If

您没有正确分配Application.Printer,如果您使用上面的内容,它将查找tempTicket中命名的打印机,如果找到,将设置Access以将其用作默认打印机。如果找不到打印机,则可以根据需要处理错误。