Changing printer orienation at runtime using and crystal reports

Posted on 2003-12-12
Last Modified: 2007-12-19
Ok, here is the situation..

I have a report in done using crystal reports and  I am using the crystal reports viewer control to display the report to the user before printing.  

The report is set ot landscape and legal size paper, and displays that way in the preview control (I have checked and it is set at every place possible within crystal).  When I print the report, however, the paper size is ALWAYS set to portrait on letter paper (The default for the printer).  While I know that I can change this and get a proper print, I know my end users will think that what they see on the screen is what they are going to get printed out.   How can I set the printers settings before the report gets printed?

so far I am at a lost.  It seems that in all the printer objects are read only, and the only way to do it is to FORCE THE END USER to set them from a dialog.  I find this stupid, to say the least.

Any answers?

Question by:matrixmedia

Expert Comment

ID: 9928749
Your report
dim m_ReportDocument as new myreport

Me.m_ReportDocument.PrintOptions.PaperOrientation = CrystalDecisions.[Shared].PaperOrientation.DefaultPaperOrientation

Expert Comment

ID: 9928757
Me.m_ReportDocument.PrintOptions.PaperSize = CrystalDecisions.[Shared].PaperSize.DefaultPaperSize

Author Comment

ID: 9928830
That doesnt do it... I get the same exact thing..when I hit print, I get the printer options dialog and  unless I manualy change the orientation and papersize, I get the printers default size.  
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Expert Comment

ID: 9928870
yeah, ok I know.

Disable the print button on the preview control.

Then use this instead

Expert Comment

ID: 9928878
Cause when you use the button on the preview control, it uses the settings that the control gives to the report, you have to trick it somehow.

You could add your own print button on the form and then you can control the printing

Author Comment

ID: 9928992
I just figured that out...and it really reailly is bad.  

I am using a single 'print preview form' that I reuse for several reports.  to setup my own print preview button, I have to have a preview form for EACH report that uses these different settings., unless there is a better and different way to do this.

Currently what I do is I load the form and set the viewer controls reportsource property to which ever crystal report I want to run.   the code looks like this:

dim rptOutput as new rptFinanciaReport
dim frmPreview as new frmRptPreview

with frmPreview
   .CrystalViewer.ReportSource = rptOutput
end with

Maybe there is a better way to do this that I am missing, but it will requier a bit of retooling for certain reports.  I dont want to have to modify this print preview form for every report that I generate, as that kind of defeats the purpose.  

Thanks for the help though.


Accepted Solution

Dranizz earned 250 total points
ID: 9929034
Create another form with options, just like a print dialogue to control the printing.
On the constructor of your form, have a boolean decide if you use the default print button or the custom.

Expert Comment

ID: 10040965
in Crystal Reports open the Report
From the file Menu select Printer Set up
unCheck the checkbox 'Use Default properties'

Expert Comment

ID: 10172610
I had a similar problem while trying to print out reports on a custom paper size with Crystal Reports. To work around this I had to do the following:

I made a dll in old vb that found the paper code for the custom paper size (code included at the end of this)

I made a reference to the dll in my app and called it, sending in the name of the custom form (or paper size) that I wanted it to print. I then used this line in my vb code to print to that paper size.

  rpt.PrintOptions.PaperSize = CType(133, CrystalDecisions.Shared.PaperSize)

Where 133 is the custom paper code, or form code, that I got from the dll.

I ended up passing in the name of my form to the dll and if it does not exist then I create it, this way the end user does not have to do anything.

I know this isn't great but I was unable to convert the vb code to as there is problems with data types, the code uses many 'any' type data declarations that does not support.

I hope this helps.


Keith Grunow

Dim FormName As String

FormName = "MyCustomForm"   ' Use special, user-defined form.
UseForm FormName
End Sub

Private Sub Command2_Click()
Dim FormName As String

' Get FormName from the ListBox.
On Error GoTo ListBoxERR    ' Trap for no selection.
FormName = Mid(List1.Text, 1, InStr(1, List1.Text, " -") - 1)
On Error GoTo 0             ' Turn off Error trap.

UseForm FormName

Exit Sub
MsgBox "Select a printer from the ListBox before using this option.", _
End Sub

Private Sub Command3_Click()
Dim RetVal As Long
Dim PrinterHandle As Long   ' Handle to printer
Dim PrinterName As String
Dim FormName As String
Dim Continue As Long

' Delete form that is selected in ListBox.
PrinterName = Printer.DeviceName  ' Current printer
If OpenPrinter(PrinterName, PrinterHandle, 0&) Then

    On Error GoTo ListBoxERR    ' Trap for no selection.
    FormName = Mid(List1.Text, 1, InStr(1, List1.Text, " -") - 1)
    On Error GoTo 0             ' Turn off Error trap.
    Continue = MsgBox("Are you sure you want to permanently remove " & _
               FormName & " from " & PrinterName & "?", vbYesNo)
    If Continue = vbYes Then
        RetVal = DeleteForm(PrinterHandle, FormName & Chr(0))
        If RetVal <> 0 Then ' DeleteForm succeeded.
            List1.Clear     ' Reflect the deletion in the ListBox.
            Form_Load       ' Rebuild the list.
            MsgBox FormName & " deleted!", vbInformation, "Success!"
            MsgBox FormName & " not deleted!" & vbCrLf & vbCrLf & _
               "Error code: " & Err.LastDllError, vbInformation, "Failure!"
        End If
    End If
    ClosePrinter (PrinterHandle)
End If

Exit Sub
MsgBox "Select a printer from the ListBox before using this option.", _
ClosePrinter (PrinterHandle)
End Sub

Private Sub Form_Load()
Dim NumForms As Long, I As Long
Dim aFI1() As FORM_INFO_1           ' Working FI1 array
Dim Temp() As Byte                  ' Temp FI1 array
Dim BytesNeeded As Long
Dim PrinterName As String           ' Current printer
Dim PrinterHandle As Long           ' Handle to printer
Dim FormItem As String              ' For ListBox
Dim RetVal As Long
Dim FormSize As SIZEL               ' Size of desired form

PrinterName = Printer.DeviceName    ' Current printer
If OpenPrinter(PrinterName, PrinterHandle, 0&) Then
    With FormSize   ' Desired page size
        .cx = 214000
        .cy = 216000
    End With
    ReDim aFI1(1)
    RetVal = EnumForms(PrinterHandle, 1, aFI1(0), 0&, BytesNeeded, _
    ReDim Temp(BytesNeeded)
    ReDim aFI1(BytesNeeded / Len(FI1))
    RetVal = EnumForms(PrinterHandle, 1, Temp(0), BytesNeeded, _
             BytesNeeded, NumForms)
    Call CopyMemory(aFI1(0), Temp(0), BytesNeeded)
    For I = 0 To NumForms - 1
        With aFI1(I)
            ' List name and size including the count (index).
            FormItem = PtrCtoVbString(.pName) & " - " & / 1000 & _
               " mm X " & / 1000 & " mm   (" & I + 1 & ")"
            List1.AddItem FormItem
        End With
    Next I
    ClosePrinter (PrinterHandle)
End If
End Sub

Private Sub UseForm(FormName As String)
Dim RetVal As Integer

RetVal = SelectForm(FormName, Me.hwnd)
Select Case RetVal
    Case FORM_NOT_SELECTED   ' 0
        ' Selection failed!
        MsgBox "Unable to retrieve From name", vbExclamation, _
           "Operation halted!"
    Case FORM_SELECTED   ' 1
        ' Selection succeeded!
        PrintTest     ' Comment this line to avoid printing
    Case FORM_ADDED   ' 2
        ' Form added and selected.
        List1.Clear   ' Reflect the addition in the ListBox
        Form_Load     ' by rebuilding the list.
End Select
End Sub


Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDependency to get update from sql to my c# app 2 36
Alert on Server memory 2 19
SQL Help 27 40
Powershell File Sort 8 24
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now