Excel Chart to Image - Invoke C#: Object reference not set to an instance of an object

Hi guys, I have an invoke code for exporting chart to image from excel. The code works fine and export chart to image file but keeps on throwing an error Object reference not set to an instance of an object

Try	
Dim Application As New Microsoft.Office.Interop.Excel.Application
Dim Workbook As Microsoft.Office.Interop.Excel.Workbook = 	Application.Workbooks.Open(GID_ReportPath+"\Charts_Combined.xlsx")
Dim WorkSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(Workbook.Sheets("Cashflow_CHART"), Microsoft.Office.Interop.Excel.Worksheet)
Dim ChartObject As Microsoft.Office.Interop.Excel.ChartObject = CType(WorkSheet.ChartObjects("Cashflow_CHART"),Microsoft.Office.Interop.Excel.ChartObject)
Dim Chart As Microsoft.Office.Interop.Excel.Chart = CType(ChartObject.Chart,Microsoft.Office.Interop.Excel.Chart)

Chart.Export(GID_ReportPath+"\Charts_Combined_Cashflow.png","PNG")
Workbook.Close(False)
Workbook = Nothing

Application = Nothing
Application.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(Application)

Catch ex As Exception
	Console.WriteLine(ex.Message)
End Try

Please help me if anyone has an idea.

Hi,

I think Application = Nothing should be removed or move to after Application.Quit()
Probably it causes “Object reference not set exception” at Applciation.Quit() as Application is null.

image

Regards,

Thanks @Yoichi , I tried moving after Application.Quit() did not work, but it worked when I commented it. Thanks a lot .

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.