Reputation: 43
I'm trying to print from a selected network printer. Sometimes it's working, but other times it won't print, giving me the following error:
"Invalid printer specified.MyCrystalRPTfilename 11124_5324_{67F07633-5EF3-49B4-9573-BB34151D75BA}.rpt"
I found the different parts of the code below from the net. I know that this was asked here before but the solutions given doesn't work for me, maybe I just missed something.
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
Upvotes: 0
Views: 1322
Reputation: 5878
Ensure that the printer you intend to use actually exists at the time of the print: Try checking with this:
if Not PrinterSettings.InstalledPrinters.OfType(Of String)().Any(Function (s) s.Equals(strDefaultPrinter)) Then
' Display/handle an error
End If
EDIT
Ok, depending on the version in use, SAP recommends changing to use the PrintOutputController
API stating that PrintToPrinter
is no longer actively developed or supported:
CrystalDecisions.ReportAppServer.Controllers
and CrystalDecisions.ReportAppServer.ClientDoc
Create a Print options object with information about the default printer
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()
Upvotes: 1