views:

343

answers:

1

hi i am adding chart to excel from c# as follows

 Excel.Worksheet ws = 
           (Excel.Worksheet) Globals.ThisAddIn.GetActiveWorksheet();
 Excel.Shape chart = 
           (Excel.Shape) ws.Shapes.AddChart(Type.Missing,100,100,100,100);

is it possible now to add let's say Rectangle on the chart above for example like

 chart.Add(msoRectangle,100,100,100,100); // smth of this kind

so that when i drag the chart, chart objects (for example above rectangle) will move with the chart... and is it possible to make it uneditable? thanks a lot!

A: 

You have to get to the Chart.Shapes collection and add a new Rectangle Shape. There is not a lot of documentation on VSTO, so you just have to play around.

Excel.Shape rectangle = (Excel.Shape)chart.Chart.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 5, 5, 50, 50);

Now, about that read-only..I am not sure if you can protect just the chart, but you can of course protect the whole WorkSheet that the chart is on.

Here is how to password protect your sheet, this is a full protection.

 //
 this.ProtectWorkSheet(ws);

 //helper method
 private void ProtectWorkSheet(Excel.Worksheet workSheet)
        {
            //protect sheet
            workSheet.Protect("yourpassword", true, true, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }
Stan R.
thanks a lot Stan!