【问题标题】:Invalid printer specified (sometimes it's working, other times it won't)指定的打印机无效(有时可以工作,有时不能)
【发布时间】:2019-04-24 06:58:25
【问题描述】:

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

“指定的打印机无效。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

【问题讨论】:

  • 看着这个,我注意到你有PrintDocument1prtdoccryRpt 前两个在做什么?另外,为了排除故障,打印机失败时和成功时的打印机名称是什么?
  • @reckface 忘了评论前两个,我没用过。故障排除时,打印机名称是我选择的,strdefaultPrinter 值是打印机名称。看起来像这样:“\\network\sharedprintername”,在给我例外之前,它在这部分结束:cryRpt.PrintOptions.PrinterName = strDefaultPrinter

标签: vb.net printing


【解决方案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()
    

【讨论】:

  • 嗨。我这样做了。它表明共享打印机存在。顺便说一句,打印机是共享网络打印机,如果这意味着什么的话。
  • 如果存在,是否仍然失败?
  • 是的,仍然失败。这就是为什么我现在不知道如何调试它,即使它得到了正确的打印机。
  • 我添加了SAP的推荐
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
  • 2012-07-07
  • 1970-01-01
相关资源
最近更新 更多