Wesley
Wesley

Reputation: 800

C# 4.0 Excel Generate Chart and Position

I ran into a little problem. I've created a code that creates a Excel sheet with a few records and creates a line chart. But the problem is I cant seem to position the chart at a specified position. Here is my code:

 excelApp.ActiveSheet.Shapes.AddChart.Select();
 excelApp.ActiveChart.ChartType = Excel.XlChartType.xlLine;
 excelApp.ActiveChart.SetSourceData(Source: excelApp.Range["Sheet1!$A$1:$B$7"]);
 excelApp.ActiveChart.Parent.Name = "mainChartView";
 excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100; 
 // or excelApp.ActiveChart.Shapes.Item("mainChartView").Top = 100;

Both return the following error:

ArgumentExecption was unhandled (The item with the specified name wasn't found).

This is the error line:

excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100; 
// or excelApp.ActiveChart.Shapes.Item("mainChartView").Top = 100;

I really hope that one of you has the correct awnser, help is very appreciated!

edit: Solution was:

excelApp.ActiveSheet.Shapes.Item("mainChartView").Top = 20;

Upvotes: 2

Views: 2441

Answers (3)

Wesley
Wesley

Reputation: 800

i've found the anwser myself.

I replaced:

excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100;

with:

excelApp.ActiveSheet.Shapes.Item("mainChartView").Top = 20;

Upvotes: 1

Jon Peltier
Jon Peltier

Reputation: 6063

You should also be able to use:

excelApp.ActiveChart.Parent.Top = 100;

Upvotes: 1

Ta01
Ta01

Reputation: 31610

You're setting the .Parent Name property, shouldn't you be setting the name of the Chart object itself? Do you have the Chart object available? Set its name property

excelApp.ActiveSheet.Shapes.AddChart should return a shape type (in your case chart). Set that types name property and then try

Upvotes: 0

Related Questions