excel chart problem

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
 
M

Marvin Varela

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top