指定的打印机无效(有时它正在工作,有时它不会)

问题描述 投票:0回答:1

我正在尝试从选定的网络打印机打印。有时它正在工作,但有时它不会打印,给我以下错误:

“指定的打印机无效.MyCrystalRPTfilename 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
vb.net printing
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.