excel chart problem

  • Thread starter Thread starter Prasun
  • Start date Start date
P

Prasun

Code:

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
oWB = oXL.Workbooks.Open("C:\data.xls") '---> Exception thrown here
'Dim ThisWorkbook As Excel.Workbook
Dim ThisWorksheet As Excel.Worksheet
ThisWorksheet = oWB.ActiveSheet

Dim charts As Excel.ChartObjects = _
CType(ThisWorksheet.ChartObjects(), Excel.ChartObjects)

' Adds a chart at x = 100, y = 300, 500 points wide and 300 tall.
Dim chartObj As Excel.ChartObject = charts.Add(100, 300, 500, 300)
Dim chart As Excel.Chart = chartObj.Chart

' Gets the cells that define the bounds of the data to be charted.
Dim chartRange As Excel.Range = ThisWorksheet.Range("A2", "B8")
chart.SetSourceData(chartRange)

chart.ChartType = Excel.XlChartType.xlXYScatter
Dim series As Excel.Series
Dim seriesCollection As Excel.SeriesCollection = _
CType(chart.SeriesCollection(), Excel.SeriesCollection)
series = seriesCollection.Item(seriesCollection.Count)
End Sub




I have the following exception thrown in the line shown above.


An unhandled exception of type 'System.NullReferenceException' occurred in
ChartDisplay.exe

Additional information: Object reference not set to an instance of an
object.


does anyone know what is wrong
 
You have not initialized oXL vairable

you have to do something like:

oXL = new Excel.Application();

You have to check if that's the right way to initialize your object or you
need to use other way, but you have to initialize it somehow.

Hope this helps.
 
Back
Top