Function ExportReport(ByVal crReportDocument As ReportDocument, ByVal PhysicalApplicationPath As String, ByVal FileName As String, ByVal format As String) As String
' This subroutine uses a case statement to determine the selected export format from the dropdownlist
' menu and then sets the appropriate export options for the selected export format. The report is
' exported to a subdirectory called "Exported".
'Check to see if the application directory has a subdirectory called "Exported".
'If not, create the directory since exported files will be placed here.
'This uses the Directory class of the System.IO namespace.
Dim ExportPath As String
ExportPath = PhysicalApplicationPath + "Exported\"
If Directory.Exists(ExportPath) = False Then
Directory.CreateDirectory(PhysicalApplicationPath + "Exported\")
End If
Dim crExportOptions As ExportOptions
Dim crDiskFileDestinationOptions As DiskFileDestinationOptions
' First we must create a new instance of the diskfiledestinationoptions class and
' set variable called crExportOptions to the exportoptions class of the reportdocument.
crDiskFileDestinationOptions = New DiskFileDestinationOptions()
crExportOptions = crReportDocument.ExportOptions
'Find the export type specified in the dropdownlist and export the report. The possible export format
'types are Rich Text(RTF), Portable Document (PDF), MS Word (DOC), MS Excel (XLS), Crystal Report (RPT),
'HTML 3.2 (HTML) and HTML 4.0 (HTML)
'
'Though not used in this sample application, there are options that can be specified for various format types.
'When exporting to Rich Text, Word, or PDF, you can use the PdfRtfWordFormatOptions class to specify the
'first page, last page or page range to be exported.
'When exporting to Excel, you can use the ExcelFormatOptions class to specify export properties such as
'the column width etc.
Select Case format 'this contains the value of the selected export format.
Case "RTF" '"Rich Text (RTF)"
'--------------------------------------------------------------------
'Export to RTF.
'append a filename to the export path and set this file as the filename property for
'the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + FileName + ".rtf"
'set the required report ExportOptions properties
With crReportDocument.ExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.RichText
.DestinationOptions = crDiskFileDestinationOptions
End With
'--------------------------------------------------------------------
Case "PDF" '"Portable Document (PDF)"
'Export to PDF
'append a filename to the export path and set this file as the filename property for
'the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + FileName + ".pdf"
'set the required report ExportOptions properties
With crExportOptions
.DestinationOptions = crDiskFileDestinationOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
End With
'--------------------------------------------------------------------
Case "DOC" '"MS Word (DOC)"
'Export to Word
'append a filename to the export path and set this file as the filename property for
'the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + FileName + ".doc"
'set the required report ExportOptions properties
With crReportDocument.ExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.WordForWindows
.DestinationOptions = crDiskFileDestinationOptions
End With
'--------------------------------------------------------------------
Case "XLS" '"MS Excel (XLS)"
'Export to Excel
'append a filename to the export path and set this file as the filename property for
'the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + FileName + ".xls"
'set the required report ExportOptions properties
With crReportDocument.ExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.Excel
.DestinationOptions = crDiskFileDestinationOptions
End With
'--------------------------------------------------------------------
Case "RPT" '"Crystal Report (RPT)"
'Export to Crystal reports:
'append a filename to the export path and set this file as the filename property for
'the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + FileName + ".rpt"
'set the required report ExportOptions properties
With crReportDocument.ExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.CrystalReport
.DestinationOptions = crDiskFileDestinationOptions
End With
End Select 'export format
'Once the export options have been set for the report, the report can be exported. The Export command
'does not take any arguments
' Try
' Export the report
crReportDocument.Export()
ExportReport = ""
' Catch Err As Exception
' ExportReport = Err.Message.ToString
' End Try