10.4 Select Whether the Report Will Be Displayed, Printed, or Exported
Using Visual Basic .NET Code
I know I can use the Crystal Report Viewer to print and export my reports, but I want to
be able to have control over that, and maybe not even include the Viewer in some cases.
Technique
For this How-To, you will use a Tab control to display the three options for the user:
Print, Export, and View (see Figure 10.18).
Figure 10.18. You have flexibility when it comes to letting your users work with
reports.
The first tab, Print, allows you to specify the number of copies to print, along with the
starting and ending pages.
Printing Using the Report Document
This page will use the PrintToPrinter method shown here:
Me.rdHowTo10_4.PrintToPrinter(Me.txtNumOfCopies.Text, False,
Me.txtStartPage.Text, Me.txtEndPage.Text)
The PrintOptions object, found on the ReportDocument, is very useful. The PrintOptions
object has the following properties: PaperOrientation, PaperSize, PaperSource,
PrinterDuplex, and PrinterName. You can set these properties either at design time using
the property sheet, or at runtime using code.
Exporting Using the Report Document
When you're exporting using the Report document, you will be using the ExportOptions
object. The ExportOptions object is made up of four other properties/objects:
•
DestinationOptions. This is made up of one of three possible objects:
DiskFileDestinationOptions, ExchangeFolderDestinationOptions, or
MicrosoftMailDestinationOptions.
•
ExportDestinationType. This gets or sets the export destination type. This will be
DiskFile, ExchangeFolder, MicrosoftMail, or NoDestinationType.
TextBox Name txtNumOfCopies
Text 1
TextBox Name txtStartPage
Text 0
TextBox Name txtEndPage
Text 0
Button Name btnPrint
Export Label Text Export Type
Label Text File Path
TextBox Name txtExportFilePath
Label Text File Name
TextBox Name txtExportFileName
ListBox Name lstExportType
Button Text btnExport
View CrystalReportViewer Anchor Top, Bottom, Left, Right
ReportSource rdHowTo10_4
5. Type Excel and Word Document into the Items collection of lstExportType.
6. Add the code in Listing 10.1 to the Click event of btnPrint.
Listing 10.1 frmHowTo10_4.vb: Printing the Report
Private Sub btnPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPrint.Click
Dim po As PrintDialog
Me.rdHowTo10_4.PrintToPrinter(Me.txtNumOfCopies.Text, False, _
Me.txtStartPage.Text, Me.txtEndPage.Text)
End Sub
7. Add the code in Listing 10.2 to the Click event of btnExport. This code tests the
value of lstExportType and assigns the appropriate ExportFormatType and file
ddo.DiskFileName = Me.txtExportFilePath.Text & _
Me.txtExportFileName.Text & strExt
.DestinationOptions = ddo
End With
Me.rdHowTo10_4.Export()
MessageBox.Show("Report Exported!")
Catch excp As Exception
MessageBox.Show(excp.Message)
End Try
End Sub
Comments
As you can see, very little code is needed to provide a great deal of functionality.
Sometimes when you're using tools such as the Viewer in a tabbed form, the tab pages
can become cluttered. If you want to limit the power of the Viewer or make it more