Solved

Changing printer orienation at runtime using vb.net and crystal reports

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

I have a report in done using crystal reports and vb.net.  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 vb.net 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?

Tony
0
Comment
Question by:matrixmedia
9 Comments
 
LVL 8

Expert Comment

by:Dranizz
ID: 9928749
Your report
dim m_ReportDocument as new myreport

Me.m_ReportDocument.PrintOptions.PaperOrientation = CrystalDecisions.[Shared].PaperOrientation.DefaultPaperOrientation
0
 
LVL 8

Expert Comment

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

Author Comment

by:matrixmedia
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.  
0
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.

 
LVL 8

Expert Comment

by:Dranizz
ID: 9928870
yeah, ok I know.


Disable the print button on the preview control.

Then use this instead
m_ReportDocument.PrintToPrinter(1,True,0,0)
0
 
LVL 8

Expert Comment

by:Dranizz
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
0
 

Author Comment

by:matrixmedia
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
   .show()
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.

0
 
LVL 8

Accepted Solution

by:
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.
0
 

Expert Comment

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

Expert Comment

by:keithg12
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 vb.net 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 vb.net as there is problems with data types, the code uses many 'any' type data declarations that vb.net does not support.

I hope this helps.

Regards,

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
ListBoxERR:
MsgBox "Select a printer from the ListBox before using this option.", _
   vbExclamation
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!"
        Else
            MsgBox FormName & " not deleted!" & vbCrLf & vbCrLf & _
               "Error code: " & Err.LastDllError, vbInformation, "Failure!"
        End If
    End If
    ClosePrinter (PrinterHandle)
End If

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

Private Sub Form_Load()
Dim NumForms As Long, I As Long
Dim FI1 As FORM_INFO_1
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, _
             NumForms)
    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) & " - " & .Size.cx / 1000 & _
               " mm X " & .Size.cy / 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

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…

832 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