指定了无效的打印机(有时它可以工作,而其他时候则不能)

时间:2019-04-24 06:58:25

标签: vb.net printing

我正在尝试从选定的网络打印机进行打印。有时它可以正常工作,但其他时候却无法打印,给我以下错误:

  

“指定了无效的打印机。MyCrystalRPT文件名11124_5324_ {67F07633-5EF3-49B4-9573-BB34151D75BA} .rpt”

我从网上找到了下面代码的不同部分。 我知道之前曾有人问过这个问题,但是给出的解决方案对我不起作用,也许我只是错过了一些东西。

Try

            Dim PrintDialog1 As New PrintDialog
            PrintDialog1.ShowDialog()
            PrintDocument1.PrinterSettings.PrinterName = PrintDialog1.PrinterSettings.PrinterName

            Dim prtdoc As New PrintDocument
            Dim strDefaultPrinter As String = PrintDialog1.PrinterSettings.PrinterName


            Dim cryRpt As New ReportDocument
            Dim crtableLogoninfos As New TableLogOnInfos
            Dim crtableLogoninfo As New TableLogOnInfo
            Dim crConnectionInfo As New ConnectionInfo
            Dim CrTables As Tables
            Dim CrTable As Table
            cryRpt.Load("C:\path\of\my\report\MyCrystalRPTfilename.rpt")

            With crConnectionInfo
                .ServerName = "myserver"
                .DatabaseName = "mydbase"
                .UserID = "myuser"
                .Password = "mypassword"
            End With

            CrTables = cryRpt.Database.Tables
            For Each CrTable In CrTables
                crtableLogoninfo = CrTable.LogOnInfo
                crtableLogoninfo.ConnectionInfo = crConnectionInfo
                CrTable.ApplyLogOnInfo(crtableLogoninfo)
            Next

            cryRpt.Refresh()
            cryRpt.PrintOptions.PrinterName = strDefaultPrinter
            cryRpt.PrintOptions.PaperSource = CrystalDecisions.[Shared].PaperSource.Auto
            cryRpt.PrintToPrinter(1, False, 1, 1)


        Catch ex As Exception
            MessageBox.Show(ex.InnerException.ToString())
        End Try

1 个答案:

答案 0 :(得分:1)

确保在打印时实际存在要使用的打印机: 尝试检查以下内容:

if Not PrinterSettings.InstalledPrinters.OfType(Of String)().Any(Function (s) s.Equals(strDefaultPrinter)) Then
  ' Display/handle an error
End If

编辑

好吧,根据所使用的版本,SAP建议更改为使用PrintOutputController API,声明PrintToPrinter不再得到积极开发或支持:

  1. 参考CrystalDecisions.ReportAppServer.ControllersCrystalDecisions.ReportAppServer.ClientDoc
  2. 使用有关默认打印机的信息创建“打印选项”对象

    Dim options = New PrintReportOptions With
    {
        .PrinterName = strDefaultPrinter,
        .Collated = False,
        .NumberOfCopies = 1,
        .JobTitle = report.Name
    }
    ' pass the options to the print method
    report.ReportClientDocument.PrintOutputController.PrintReport(options)
    ' If you're done
    report.Close()
    report.Dispose()