I always get an error with Line 39. It always have NullPointerException error. Could you check what is the error? Here's my code


Imports System.Runtime.InteropServices

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filen As String

filen = TextBox1.Text
test(filen)

End Sub

Sub test(byval filen)

Dim oApp As Microsoft.Office.Interop.Excel.Application

oApp = New Microsoft.Office.Interop.Excel.Application
Dim oldCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")
oApp.Workbooks.Add()
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI

Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
Dim wc As Microsoft.Office.Interop.Excel.Chart

Try

excel = New Microsoft.Office.Interop.Excel.Application
wb = excel.Workbooks.Open(filen)
excel.Visible = True
wb.Activate()
ws = New Microsoft.Office.Interop.Excel.Worksheet
ws = wb.Worksheets("Sheet1")
wc = New Microsoft.Office.Interop.Excel.Chart
wc = ws.ChartObjects(1).Activate

wc.ActiveChart.Export(Filename:="C:\Users\mark.perfinan\Documents\trusted\" + wc.ActiveChart.Name + ".png", FilterName:="PNG")

Catch ex As COMException
MessageBox.Show("Error accessing Excel: " + ex.ToString())

Catch ex As Exception
MessageBox.Show("Error: " + ex.ToString())

End Try

'wb.Worksheets().ChartObjects().Chart.Export(filename:="C:\Users\mark.perfinan\Documents\trusted\" + wb.Worksheets(ActiveSheet.Index).ChartObjects().Chart.Name + ".png", filtername:="PNG")
'ActiveChart.Export(filename:="C:\Users\xxxxxxxx\Documents\trusted\" + ActiveChart.Name + ".png", filterName:="png")

End Sub

End Class

dont know what line 39 is since you didnt use code tags for your post but if i would have to guess i would say wb.Worksheets(ActiveSheet.Index).ChartObjects().Chart.Name + ".png", filtername:="PNG") should be wb.Worksheets(ActiveSheet.Index).ChartObjects(0).Chart.Name + ".png", filtername:="PNG")

dont know what line 39 is since you didnt use code tags for your post but if i would have to guess i would say wb.Worksheets(ActiveSheet.Index).ChartObjects().Chart.Name + ".png", filtername:="PNG") should be wb.Worksheets(ActiveSheet.Index).ChartObjects(0).Chart.Name + ".png", filtername:="PNG")

I left that part as a comment.

Here is line 39:
wc.ActiveChart.Export(Filename:="C:\Users\mark.perfinan\Documents\trusted\" + wc.ActiveChart.Name + ".png", FilterName:="PNG")


I think there is something wrong with the declaration part. What do you think?

uhm sorry but in the code you pasted the line wasn't comment :$

anyway if you debug till the line wc = ws.ChartObjects(1).Activate and check after passing this line what value wc got then you would be closer.
might the index is just wrong

uhm sorry but in the code you pasted the line wasn't comment :$

anyway if you debug till the line wc = ws.ChartObjects(1).Activate and check after passing this line what value wc got then you would be closer.
might the index is just wrong

I tried changing the index but I got another error. Here's the error:

"Error Accessing Excel: System.Runtime.InteropServices.COMException at Microsoft.Office.Interop.Excel._Worksheet.ChartObjects(Object Index) at WindowsApplication1.Form1.test(Object filen) in C:\Documents\VS2008\Projects\WindowsApplication1\Form1.vb:line 37"


line 37 contains the code "wc = ws.ChartObjects(0).Activate"

BTW, thanks for the reply, really appreciated it. I'm just new in programming in vb

Just analyze what are you doing on this piece of code

1 - excel = New Microsoft.Office.Interop.Excel.Application
2 - wb = excel.Workbooks.Open(filen)
3 - excel.Visible = True
4 - wb.Activate()
5 - ws = New Microsoft.Office.Interop.Excel.Worksheet
6 - ws = wb.Worksheets("Sheet1")
7 - wc = New Microsoft.Office.Interop.Excel.Chart
8 - wc = ws.ChartObjects(1).Activate

On step 1 you create a new wxcel application
On step 2 you open an already existing excel workbook on file
On step 3 you make Excel visible if it wasn't.
On step 4 you activate the opened workbook
On step 5 you create a new worksheet, alone, not in the workbook.
On step 6 you override the new created worksheet with one named "Sheet1" supposed to exist on the workbook. (Is this true?)
On step 7 you create a new char, alone, not in the workbook nor in the sheet.
On step 8 you override the new chart with one (index 1 of the charobjects) supposed already exists on the 'Sheet1' wich in fact is not proven to exist.

So, basically, you are activating an object that, most probably, does not exist on the workbook you open.

If wat you want is to create new sheets and charts, you need to add the sheet to the corresponding collections of workbook sheets, and then the chart to the char objects collection.

Hope this helps
PD: see http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/098bcfb4-d228-405a-84f8-810ceab58d78/

I deleted number 5 and 7 but I still get the same error on the same line. There is an existing excel file that has charts on it. How can I access the charts inside the excel file?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.