Not able to add title to the chart programmetically

V

vineetbatta

I have trying for some time now to add Title to the chart which i have created using the following code but it throws COM error -

System.Runtime.InteropServices.COMException: Unable to set the Text property of the ChartTitle class

The code is as following ...

Excel.Sheets sheets = thisWorkbook.Worksheets;
Excel.Worksheet thisWorksheet = (Excel.Worksheet)sheets.get_Item(1);

thisWorksheet = thisWorkbook.ActiveSheet as Excel.Worksheet;
Excel.ChartObjects charts =(Excel.ChartObjects)thisWorksheet.ChartObjects(Type.Missing);


Excel.ChartObject chartObj = charts.Add(100, 300, 500, 300);

Excel.Chart chart = chartObj.Chart ;

Excel.ChartTitle cht = chart.ChartTitle;
cht.Text = "Report";

If i remove the chartTitle code above (2 line) , it works fine.
Complete code not shown here for simplicity.

I also tried directly

Chart.ChartTitle.text = "Report";
AND
Chart.ChartTitle.caption = "Report";

Infact any thing related to chartTitle object i am not able to set at all.
Please let me know of any pointers.

Thanx in advance
 
J

Jon Peltier

I don't really get your code, it's C++ or something, right? But I do
know you have to use

Chart.HasTitle = True

before you use

Chart.ChartTitle.Text = "Whatever"

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______
 

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