4 Replies - 2854 Views - Last Post: 23 October 2012 - 09:24 AM Rate Topic: -----

#1 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 221
  • View blog
  • Posts: 1,030
  • Joined: 25-June 12

.ExportToDisk Not Set to Object Instance Error

Posted 22 October 2012 - 02:48 PM

Hello Everyone,

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



Error Image:
[attachment=31381:image.png]

Is This A Good Question/Topic? 0
  • +

Replies To: .ExportToDisk Not Set to Object Instance Error

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: .ExportToDisk Not Set to Object Instance Error

Posted 22 October 2012 - 10:46 PM

Try using keyword New when you instantiate RPT in line 11.
Was This Post Helpful? 0
  • +
  • -

#3 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 221
  • View blog
  • Posts: 1,030
  • Joined: 25-June 12

Re: .ExportToDisk Not Set to Object Instance Error

Posted 23 October 2012 - 06:01 AM

View Postlucky3, on 22 October 2012 - 10:46 PM, said:

Try using keyword New when you instantiate RPT in line 11.


Thanks for the reply lucky3. That seems to have solved my object reference issue, but now I am getting an "Invalid report file path" on the same line. Any ideas on this one? I've changed the file path to different locations but keep getting the same error.

I was told by a Developer in another section that it might be viable (Prevent file lock-ups) for me to use Memory Streams to perform the action I'm trying to do instead of saving the file to disk, worrying about paths, and deleting the files, but I have not used Memory Streams before myself.

Just an idea, in any case I'm still trying to figure out why my current code doesn't work like it did a few weeks before.

    Private Sub btnEmailReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmailReport.Click
        'Declaration for where the file will be stored before exporting to Desktop, Attaching to Email,
        ' and then removing from desktop.
        Dim RPT As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim strFilePath As String = "C:\Users\" & Environment.UserName & "\Desktop\Report.pdf"
        RPT.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, strFilePath)
        'Check if file Exists already
        If System.IO.File.Exists(strFilePath) Then
            MessageBox.Show("File Exists!")
        Else
            MessageBox.Show("File Doesn't Exist!")
            '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


[attachment=31398:image2.png]
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: .ExportToDisk Not Set to Object Instance Error

Posted 23 October 2012 - 08:17 AM

I think you are missing the part, where you load actual report file. You should probably have somewhere before exporting something like:
Dim myCrystalReportsFilePath As String = Application.StartupPath & "\Report.rpt" 'wherever the file is
'and then 
RPT.Load(myCrystalReportsFilePath)



Put your code in Try...Catch block, and examine exception details. Use breakpoints and step through the code.

This post has been edited by lucky3: 23 October 2012 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 221
  • View blog
  • Posts: 1,030
  • Joined: 25-June 12

Re: .ExportToDisk Not Set to Object Instance Error

Posted 23 October 2012 - 09:24 AM

View Postlucky3, on 23 October 2012 - 08:17 AM, said:

I think you are missing the part, where you load actual report file. You should probably have somewhere before exporting something like:
Dim myCrystalReportsFilePath As String = Application.StartupPath & "\Report.rpt" 'wherever the file is
'and then 
RPT.Load(myCrystalReportsFilePath)



Put your code in Try...Catch block, and examine exception details. Use breakpoints and step through the code.


The report is loading up just fine (Load procedure below) and after trying the Try/Catch it still gives me the same invalid report file path :hammer: I need the Exported file to be the one I am opening in the reportViewer. Previously I had the export working, but it was always a blank, new, file with the way I had it coded.

Load:
    Private Sub frmReportView_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim RPT As CrystalDecisions.CrystalReports.Engine.ReportDocument = Nothing

            Select Case ReportType
                Case RptType.Invoices
                    RPT = New rptInvoice
                    If _ds_rpt Is Nothing Then
                        DSReport = New dsReports
                        Dim adptInvo As New dsReportsTableAdapters.InvoiceTestTableAdapter
                        If AcctID < 0 Then
                            adptInvo.FillNonPrinted2(_ds_rpt.InvoiceTest)
                        Else
                            adptInvo.FillByAcct2(_ds_rpt.InvoiceTest, _acct_id)
                        End If
                    End If
                    If _ds_rpt.InvoiceTest.Rows.Count < 1 Then
                        MsgBox("No records to print.", MsgBoxStyle.Information, "Exiting...")
                        Me.Close()
                    End If
                    Me.Text = "Account Statements"
            End Select
            RPT.SetDataSource(_ds_rpt)
            reportViewer.ReportSource = RPT
        Catch ex As Exception
            MsgBox("Print Report load procedure failed with message:  " & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "frmReportView")
            End
        End Try
    End Sub


Export:
    Private Sub btnEmailReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmailReport.Click
        Try
            'Declaration for where the file will be stored before exporting to Desktop, Attaching to Email,
            ' and then removing from desktop.
            Dim RPT As New CrystalDecisions.CrystalReports.Engine.ReportDocument
            Dim strFilePath As String = "C:\\Users\\" & Environment.UserName & "\\Desktop\\Report.pdf"
            'RPT.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, strFilePath)
            'Check if file Exists already
            'If System.IO.File.Exists(strFilePath) Then
            '    MessageBox.Show("File Exists!")
            'Else
            '    MessageBox.Show("File Doesn't Exist!")
            '    'RPT.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, strFilePath)
            'End If

            RPT.ExportToDisk(ExportFormatType.PortableDocFormat, "C:\TEST\Report.pdf")
            ' 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.To = ""
            mail.Subject = "TESTING"
            mail.Body = "THIS IS A TEST"
            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)
        Catch ex As Exception
            MsgBox("Print Report load procedure failed with message:  " & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "frmReportView")
            End
        End Try
    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1