Copy chart from Excel to Word

C

Claus Nielsen

Hello everyone

I'm having problems with copying chart from an excel workbook to word, using
C# in a class I've created.

I create the charts dynamically, and do stuff like:

---------------------------------------------------------------------------
Excel.Chart selfLedelseChart;
selfLedelseChart = (Excel.Chart)oExcelApplic.Charts.Add(Type.Missing,
Type.Missing, Type.Missing, Type.Missing);

Excel.Range rng = (Excel.Range)ledelseSelf.Cells.get_Range("A1:B" +
(ledelseSelfRow-1).ToString(), Type.Missing);
selfLedelseChart.HasLegend = false;
selfLedelseChart.ChartType = Excel.XlChartType.xlRadarMarkers;
selfLedelseChart.SetSourceData(rng, Excel.XlRowCol.xlColumns);
---------------------------------------------------------------------------

The creation process works fine, and I can paste the chart into a workbook
using:

---------------------------------------------------------------------------
selfLedelseChart.Location(Excel.XlChartLocation.xlLocationAsObject,
ledelseSelf.Name);
---------------------------------------------------------------------------

And this is where my problem begins. After the insertion I can't use the
variable selfLedelseChart anymore. It kind of loses focus or something like
that...

So I created a makro:
---------------------------------------------------------------------------
ActiveSheet.ChartObjects("Diagram 2").Activate
ActiveChart.ChartArea.Select
---------------------------------------------------------------------------
And tried to translate that into C#, but I can't really do it. I get an
error saying the specified cast is not allowed. I tried several solutions,
but I can't get a variable to point to the chart after I've inserted it into
the chart.
I can use the code:

---------------------------------------------------------------------------
Excel.ChartObject chartObject =
(Excel.ChartObject)ledelseSelf.ChartObjects(1);
---------------------------------------------------------------------------
But I can't go anywhere from there. I need the charts in 2 places in the
workbook total, so I also need to copy them internally, because I need to
paste charts on top of oneanother.

The funny part is that I can resize the object after insertion:

---------------------------------------------------------------------------
ledelseSelf.Shapes.Item(1).Width = chartWidth;
ledelseSelf.Shapes.Item(1).Height = chartHeight;
ledelseSelf.Shapes.Item(1).Top = chartTop;
ledelseSelf.Shapes.Item(1).Left = chartLeft;
---------------------------------------------------------------------------

So I'm looking for a way to get a variable to point to a chart in a certain
worksheet, and a way to copy to word in C#. Does anyone know hoe to do this?
Or can anyone point me in the right direction? Or can you tell me where to
get help?

Please help!!!

Thanks!

Regards
 
C

Claus Nielsen

Hello everyone

I've managed to do it myself.

Just after you create the chart, insert into to worksheet, and then
reference it again. From then on you can style it to whatever you want:
------------------------
//Variables
Excel.Chart selfLedelseChart;
Excel.WorkSheet ledelseSelf;
//Do stuff
selfLedelseChart = (Excel.Chart)oExcelApplic.Charts.Add(Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
Excel.Range rng = (Excel.Range)ledelseSelf.Cells.get_Range("A1:B20,
Type.Missing);
selfLedelseChart.ChartType = Excel.XlChartType.xlRadarMarkers;
selfLedelseChart.SetSourceData(rng, Excel.XlRowCol.xlColumns);
//Paste the chart and then reference it again:
selfLedelseChart.Location(Excel.XlChartLocation.xlLocationAsObject,
ledelseSelf.Name);
selfLedelseChart = oExcelApplic.ActiveChart;
------------------------
From here on your can do whatever you want with selfLedelseChart... You can
even copy it.

Hope this helps someone.

Regards
Claus Nielsen
 

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