Hopeful some kind expert can be of assistance. I have a VB.NET project where I am attempting to enable users to open crystal reports in the ReportViewer control, and on button click, open a new Outlook email message with the Report pre-attached.
Previously, when making a quick attempt at the issue, my below code was functioning, but now I am getting an error of:
"Object reference not set to an instance of an object." Image attached.
The strange thing is, if I manually Export the file to my Desktop as "Report.pdf" and then click Email, the code functions perfectly. Logically what this semi-does/should do is on click create a disk copy of the Report exported as a .pdf file, open new email with Report.pdf attached, then delete the file from disk.
Can anyone spot what I am doing wrong or a way around what I'm starting to guess is a very elementary issue that I am overlooking?
'Imports Imports Outlook = Microsoft.Office.Interop.Outlook Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared 'Declaration Public objReportName As CrystalDecisions.CrystalReports.Engine.ReportDocument Private Sub btnEmailReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmailReport.Click Dim RPT As CrystalDecisions.CrystalReports.Engine.ReportDocument = objReportName Dim strFilePath As String = "C:\Users\" & Environment.UserName & "\Desktop\Report.pdf" 'Check if file Exists already If System.IO.File.Exists(strFilePath) Then MessageBox.Show("File Exists!") Else MessageBox.Show("File Doesn't Exist!") ' NULL REFERENCE IS ON THE BELOW LINE OF CODE! RPT.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, strFilePath) End If ' Code to open new Email message dialog with Report already attached. Dim objApp As Outlook.Application = New Outlook.Application Dim mail As Outlook.MailItem = Nothing mail = DirectCast(objApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem) mail.Attachments.Add(DirectCast(strFilePath, Object), Outlook.OlAttachmentType.olEmbeddeditem, 1, DirectCast("Attachment", Object)) mail.Display() ' Delete the PDF file from users Desktop. System.IO.File.Delete(strFilePath) End Sub