Solved

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

Posted on 2003-12-12
9
2,509 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

20 Experts available now in Live!

Get 1:1 Help Now