Visual Basic & Crystal Reports - PrintToPrinter not to correct printer and tray

We have a (in house build) application in Visual Basic. We use Visual Basic 2008 with the Crystal  Reports 2008 Runtime

It use Crystal Reports and print document to specifics printers and also to different trays (some documents are A4, some documents A3 and other documents we print on preprinted paper).

It works fine, but now we upgraded our project to Visual Studio 2013 in combination with Crystal Reports 13 runtime.
The problem is when we print a document in the new application, it print always on the default printer. We try a lot of examples and one time it prints to the correct printer, but not to the correct tray.

Our (old, working) code:

Dim PrinterSettings As New System.Drawing.Printing.PrinterSettings
Dim  doc1 As ReportDocument
Dim trayName As String
Dim RptSource As System.Drawing.Printing.PaperSource
Dim numberCopies as integer = 1

doc1.Load(reportLocation)
printerName = "name of the printer"
trayName = "Tray 3"

PrinterSettings.PrinterName = printerName
doc1.PrintOptions.PrinterName = printerName

'Loop through trays of the specific printer
For Each RptSource In PrinterSettings.PaperSources
                            If RptSource.SourceName.ToString.ToUpper = trayName.ToUpper Then
                                doc1.PrintOptions.CustomPaperSource = RptSource
                                                           
                                doc1.PrintToPrinter(numberCopies, False, 0, 0)
                            End If
 Next

Open in new window



Can someone help me with this problem? I hope so...
Program1981Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
You can try to set the default printer before sending the job to print: http://emoreau.com/Entries/Articles/2005/03/Setting-Windows-default-printer.aspx
0
Program1981Author Commented:
But I do that. I set the printer and tray before the PrintToPrinter command (it send the job to the printer)
0
Éric MoreauSenior .Net ConsultantCommented:
there are many ways of settings the default printer and not all of them are working properly under all circumstances.

I had an issue similar to yours and found that the WScript.Network mechanism (as shown in my article) was the only one working for me.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Program1981Author Commented:
Ok, I'll try your example.

I can find on the Internet there are a lot of problems with PrintToPrinter with the new(er) Crystal Version(s), but I can't find an answer yet.
0
mlmccCommented:
I assume there were no other major changes like Windows version, new printers, etc that occurred with the change to VS2013?  

I assume you also updated to use the VS2013 CR.Net assemblies.  CR2013 does not come with an SDK so you have to use the CR.Net assemblies for VS.  You need to update to SP 9 for use with VS2013.

mlmcc
0
Ido MilletProfessor of MIS at Penn State Erie and Owner, Millet SoftwareCommented:
Is your report using a custom paper size?
0
Program1981Author Commented:
I have already installed SP10.

Some reports use a custom paper size.
0
mlmccCommented:
Is the custom paper size installed on all printers?

mlmcc
0
Program1981Author Commented:
Yes, but I try it first with reports with non custom paper size.
And this reports has the same problems.
0
mlmccCommented:
I assume you are using machines that the other versions works on correctly.

The only thing I can really think of would be to try using emoreau's code to set the default printer and settings before you run the report.  It should print correctly then.

mlmcc
0
Program1981Author Commented:
Thank you for all your information. It works now with the following code:

        Dim strOldPrinter As String
        Dim WshNetwork As Object
        Dim pd As New PrintDocument

        Dim printersettings As New System.Drawing.Printing.PrinterSettings
        Dim printerName As String = "\\servername\MYPRINTERNAME"

        Try
            strOldPrinter = pd.PrinterSettings.PrinterName
            WshNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")
            WshNetwork.SetDefaultPrinter(printerName)
            pd.PrinterSettings.PrinterName = printerName
            
            PrinterSettings.PrinterName = printerName
           
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Open in new window


I can apply the paper and tray on the crystal reports report.

I have one problem/question left:

Set the default printer
WshNetwork.SetDefaultPrinter(printerName)

Open in new window

takes about 6 seconds. I have this problem on several pc's. Is there anything I can do to speed this up?
Now it takes more than 20 seconds to print this report (6 seconds for set the printer + printing report + 6 seconds to change the default printer back to normal)
0
Éric MoreauSenior .Net ConsultantCommented:
Welcome to Windows! It takes some time to propagate the change to various parts and you don't have the control over it.

Does it take that long before you see the green check mark on the screen? Have you tried just set the default and launch the printing right away?

I change the default printer in one of my server application. I never noticed that issue because I can set it when the application starts and don't have to change it back.
0
Program1981Author Commented:
Yes it takes 6 seconds before I can see the green check mark on the printer.

We print a lot of different documents (on preprinted ) on different printers the whole day... So this takes (too) much time.
0
Éric MoreauSenior .Net ConsultantCommented:
>>Yes it takes 6 seconds before I can see the green check mark on the printer

That doesn't mean that your application is not aware of the new default printer. Maybe you can loop until your default PD.DefaultPageSettings.PrinterSettings.PrinterName in your application is correctly set. You could probably also move the setting of the default printer earlier in your process (like before gathering data) so that Windows has time to propagate the change.
0
Program1981Author Commented:
Problem is solved with the following code:

Dim rpt As New ReportDocument
Dim reportName As String = ""
Dim reportPaperSize As String = ""
Dim reportTray As String = ""
Dim reportPrinterName As String = ""
Dim reportCopies As Integer = 1
reportName = "report.rpt"
reportPaperSize = "A4"
reportTray = "TRAY 4"
reportPrinterName = "PrinterName"
rpt.Load(reportName)
Dim pDoc As New System.Drawing.Printing.PrintDocument()
Dim printLayout As New CrystalDecisions.Shared.PrintLayoutSettings()
Dim printerSettings As New System.Drawing.Printing.PrinterSettings()
printerSettings.PrinterName = reportPrinterName
Dim pSettings As New System.Drawing.Printing.PageSettings(printerSettings)
Dim rptSource As New System.Drawing.Printing.PaperSource
Dim rptPaperSize As New System.Drawing.Printing.PaperSize
rpt.PrintOptions.DissociatePageSizeAndPrinterPaperSize = True
rpt.PrintOptions.PrinterDuplex = PrinterDuplex.Simplex
For Each rptSource In printerSettings.PaperSources
If rptSource.SourceName.ToString.ToUpper.Trim = reportTray Then
rpt.PrintOptions.CustomPaperSource = rptSource
pSettings.PaperSource = rptSource
End If
Next
For Each rptPaperSize In printerSettings.PaperSizes
If rptPaperSize.PaperName.ToString.ToUpper.StartsWith(reportPaperSize) Then
pSettings.PaperSize = rptPaperSize
End If
Next
printerSettings.Copies = reportCopies
rpt.PrintToPrinter(printerSettings, pSettings, False, printLayout)
End Sub

Open in new window


Documents prints quick to correct Printer & Tray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Program1981Author Commented:
After long way of programming & testing I found a solution for this problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.