我正在尝试从选定的网络打印机打印。有时它可以工作,但有时它不会打印,给我以下错误:
"指定的打印机无效。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:pathofmyreportMyCrystalRPTfilename.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
确保您打算使用的打印机在打印时确实存在: 尝试检查以下内容:
if Not PrinterSettings.InstalledPrinters.OfType(Of String)().Any(Function (s) s.Equals(strDefaultPrinter)) Then
' Display/handle an error
End If
编辑
好的,根据使用的版本,SAP 建议更改为使用PrintOutputController
API,说明不再主动开发或支持PrintToPrinter
:
- 参考
CrystalDecisions.ReportAppServer.Controllers
和CrystalDecisions.ReportAppServer.ClientDoc
使用有关默认打印机的信息创建打印选项对象
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()