Creating chart from vb.net

  • Thread starter Thread starter Reinhard
  • Start date Start date
R

Reinhard

Hello,

i want to add a chart to a Excel-Sheet with vb.net. (VS 2005). This is
possible, but on setting the HasTitle property i get an exception: "Exception
from HRESULT: 0x800A03EC".
If anybody could help me I would be very thankfull.

I have installed Microsoft Excel 2003 (11.8105.8107) SP2.
I added a reference to the "Microsoft Excel 11.0 Object Library", Version
1.5 from the COM tab. This creates following references to my project:
-Excel, Name: Microsoft.Office.Interop.Excel, Version: 1.5.0.0 (Description:
Microsoft Excel 11.0 Object Library)
- Microsoft.Office.Core, Name: Office, Version: 2.3.0.0 (Description:
Microsoft Office 11.0 Object Library)

Here is the code to reproduce the probleme:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim MyWorkbook As Microsoft.Office.Interop.Excel.Workbook
Dim MyChart As Microsoft.Office.Interop.Excel.ChartObject
Dim ChartSheet As Microsoft.Office.Interop.Excel.Worksheet

Try
ExcelApp.Visible = True
MyWorkbook = ExcelApp.Workbooks.Open("C:\MyWorkbook.xls")
ChartSheet = MyWorkbook.Sheets(1)
MyChart = ChartSheet.ChartObjects.Add(Left:=50, Width:=800,
Top:=50, Height:=500)
MyChart.Chart.ChartType = XlChartType.xl3DBarClustered
MyChart.Chart.HasTitle = True 'this line causes the error

Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub

Thank you in advance for your help

Reinhard
 
Hi Reinhard,

Excel charts need to have at least one data series before you can set the
title, try adding the line of code below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim MyWorkbook As Microsoft.Office.Interop.Excel.Workbook
Dim MyChart As Microsoft.Office.Interop.Excel.ChartObject
Dim ChartSheet As Microsoft.Office.Interop.Excel.Worksheet

Try
ExcelApp.Visible = True
MyWorkbook = ExcelApp.Workbooks.Open("C:\MyWorkbook.xls")
ChartSheet = MyWorkbook.Sheets(1)
MyChart = ChartSheet.ChartObjects.Add(Left:=50, Width:=800,
Top:=50, Height:=500)
MyChart.Chart.ChartType = XlChartType.xl3DBarClustered
MyChart.Chart.SeriesCollection.Add
Source:=MyWorkbook.Worksheets(1).Range("A1:A2")

MyChart.Chart.HasTitle = True 'this line causes the error

Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub

Ed Ferrero
www.edferrero.com
 
Back
Top