[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-08-08
16
Medium Priority
?
2,833 Views
Last Modified: 2014-09-14
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...
0
Comment
Question by:Program1981
  • 8
  • 4
  • 3
  • +1
16 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40248831
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
 

Author Comment

by:Program1981
ID: 40248837
But I do that. I set the printer and tray before the PrintToPrinter command (it send the job to the printer)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40248856
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 

Author Comment

by:Program1981
ID: 40248914
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 40249003
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
 
LVL 23

Expert Comment

by:Ido Millet
ID: 40249188
Is your report using a custom paper size?
0
 

Author Comment

by:Program1981
ID: 40249496
I have already installed SP10.

Some reports use a custom paper size.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 40249525
Is the custom paper size installed on all printers?

mlmcc
0
 

Author Comment

by:Program1981
ID: 40249531
Yes, but I try it first with reports with non custom paper size.
And this reports has the same problems.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 40249847
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
 

Author Comment

by:Program1981
ID: 40270067
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40270076
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
 

Author Comment

by:Program1981
ID: 40270110
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40270143
>>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
 

Accepted Solution

by:
Program1981 earned 0 total points
ID: 40313923
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
 

Author Closing Comment

by:Program1981
ID: 40321541
After long way of programming & testing I found a solution for this problem.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question