Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-12-12
9
Medium Priority
?
2,534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

730 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