Hey,

So I am very new to using VB, Visual Studio etc.

I am trying to do something really simple. Basically I have an Excel 2003 workbook with some data pre stored. All I need to do is add a chart to the spreadsheet and plot some data. However I can't seem to get the thing to work.

I am currently using an "Excel Addin" template however I can always change this based on suggestions. The code I currently have is as follows:

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        ' Start of VSTO generated code
        Me.Application = CType(Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(GetType(Excel.Application), Me.Application), Excel.Application)
        ' End of VSTO generated code


        Me.Application.Workbooks.Open("C:\test.xls")

        Me.Application.ActiveWorkbook.Charts.Add()
        Dim cells As Excel.Range


        cells = Me.Application.ActiveWorkbook.ActiveSheet.Range("B5","B30")

        Me.Application.ActiveWorkbook.ActiveChart.SetSourceData(cells)


    End Sub

Basically the Code is able to open the Excel file and add a chart. But I can't seem to be able to set the source data properly.

I'd appreciate a few easy examples that I can use with VSS 2008 and EXcel 2003.

I believe when you create the new Chart, it becomes the new ActiveSheet. Therefore you are trying to retrieve a Range from the Chart. Try creating the Chart after getting the Range.

This article has been dead for over six months. Start a new discussion instead.