I have realiazide that the code only works when your mail program, Microsoft Outlook is open. If it is not, that is when you get an exception. Should this be the case, i.e. you have to have your mail program open before you run the export code? But however, the file exported to my mail program is not a pdf but a text file which is unreadable.
Please find below the code i am using.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
myReport.Load("..\..\Grouping.rpt")
'ExportToPdfRtfWord("Grouping.doc", myReport, False, 0, 0)
'ExportToExcel("Grouping.xls", myReport, False, 0, True)
'ExportToHTML("Crystal Html Files", "Employees", myReport, True, True)
ExportToEmail(myReport, "crimbo@ecoweb.co.zw", "", "testing", "See if this works", "crimbo", "Hd82vp")
'ExportToExchange(myReport, "kdkd", "a", "b")
End Sub
Public Sub ExportToPdfRtfWord(ByVal Filename As String, ByRef myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal UsePageRange As Boolean, ByVal FirstPageNumber As Integer, ByVal LastPageNumber As Integer)
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
'Set the export to a disk file
myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
Dim myDestinationOptions As CrystalDecisions.Shared.DiskFileDestinationOptions
myDestinationOptions = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions
myDestinationOptions.DiskFileName = Filename
myExportOptions.ExportDestinationOptions = myDestinationOptions
'Change the next line if you want the format to be RTF or Word
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows
'The following lines stay the same regardless of formatting
Dim myFormatOptions As CrystalDecisions.Shared.PdfRtfWordFormatOptions
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreatePdfRtfWordFormatOptions()
myFormatOptions.UsePageRange = UsePageRange
If UsePageRange Then
myFormatOptions.FirstPageNumber = FirstPageNumber
myFormatOptions.LastPageNumber = LastPageNumber
End If
'Assign the formatting properties to the report and export it
myExportOptions.FormatOptions = myFormatOptions
myReport.Export(myExportOptions)
End Sub
Public Sub ExportToExcel(ByVal Filename As String, ByRef myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal UseConstantColumnWidth As Boolean, ByVal ColumnWidth As Integer, ByVal UseColumnHeadings As Boolean)
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
'Set the export to a disk file
myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
Dim myDestinationOptions As CrystalDecisions.Shared.DiskFileDestinationOptions
myDestinationOptions = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions
myDestinationOptions.DiskFileName = Filename
myExportOptions.ExportDestinationOptions = myDestinationOptions
'Set the Excel formatting properties
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.ExcelRecord
Dim myFormatOptions As CrystalDecisions.Shared.ExcelFormatOptions
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreateExcelFormatOptions()
myFormatOptions.ExcelConstantColumnWidth = ColumnWidth
myFormatOptions.ExcelUseConstantColumnWidth = UseConstantColumnWidth
myFormatOptions.ExcelTabHasColumnHeadings = UseColumnHeadings
'Assign the formatting properties to the report and export it
myExportOptions.FormatOptions = myFormatOptions
myReport.Export(myExportOptions)
End Sub
Public Sub ExportToHTML(ByVal HTMLBaseFolderName As String, ByVal Filename As String, ByRef myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal EnableSeparatedPages As Boolean, ByVal HasPageNavigator As Boolean)
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
'Set HTML specific formatting properties
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40
Dim myOptions As CrystalDecisions.Shared.HTMLFormatOptions
myOptions = CrystalDecisions.Shared.ExportOptions.CreateHTMLFormatOptions()
myOptions.HTMLBaseFolderName = HTMLBaseFolderName
myOptions.HTMLFileName = Filename
myOptions.HTMLHasPageNavigator = HasPageNavigator
myOptions.HTMLEnableSeparatedPages = EnableSeparatedPages
'Assign the formatting properties to the report and export it
myExportOptions.FormatOptions = myOptions
myReport.Export(myExportOptions)
End Sub
Public Sub ExportToExchange(ByVal myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal FolderPath As String, ByVal Password As String, ByVal Profile As String)
'Set the destination type to ExchangeFolder
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.ExchangeFolder
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
'Instantiate an ExchangeFolder options object and set its properties
Dim myOptions As CrystalDecisions.Shared.ExchangeFolderDestinationOptions
myOptions = CrystalDecisions.Shared.ExportOptions.CreateExchangeFolderDestinationOptions()
myOptions.DestinationType = CrystalDecisions.Shared.ExchangeDestinationType.ExchangePostDocMessage
myOptions.FolderPath = FolderPath
myOptions.Password = Password
myOptions.Profile = Profile
myExportOptions.DestinationOptions = myOptions
myReport.Export(myExportOptions)
End Sub
Public Sub ExportToEmail(ByVal myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal MailTo As String, ByVal CCList As String, ByVal Subject As String, ByVal Message As String, ByVal UserName As String, ByVal Password As String)
'Set the destination type to Email
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
'Instantiate an Email options object and set its properties
Dim myOptions As CrystalDecisions.Shared.MicrosoftMailDestinationOptions
myOptions = CrystalDecisions.Shared.ExportOptions.CreateMicrosoftMailDestinationOptions()
myOptions.UserName = "crimbo"
myOptions.Password = "Hd82vp"
myOptions.MailSubject = "Testing"
myOptions.MailMessage = "See if this works"
myOptions.MailToList = "crimbo@ecoweb.co.zw"
myOptions.MailCCList = ""
'Assign the options object to the report
myExportOptions.DestinationOptions = myOptions
myReport.Export(myExportOptions)
End Sub
End Class