Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2537
  • Last Modified:

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

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
matrixmedia
Asked:
matrixmedia
1 Solution
 
DranizzCommented:
Your report
dim m_ReportDocument as new myreport

Me.m_ReportDocument.PrintOptions.PaperOrientation = CrystalDecisions.[Shared].PaperOrientation.DefaultPaperOrientation
0
 
DranizzCommented:
Me.m_ReportDocument.PrintOptions.PaperSize = CrystalDecisions.[Shared].PaperSize.DefaultPaperSize
0
 
matrixmediaAuthor Commented:
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
DranizzCommented:
yeah, ok I know.


Disable the print button on the preview control.

Then use this instead
m_ReportDocument.PrintToPrinter(1,True,0,0)
0
 
DranizzCommented:
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
 
matrixmediaAuthor Commented:
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
 
DranizzCommented:
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
 
kaleem01_pkCommented:
in Crystal Reports open the Report
From the file Menu select Printer Set up
unCheck the checkbox 'Use Default properties'
0
 
keithg12Commented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now