views:

109

answers:

1

I'm having massive problems when trying to update a chart through MSWord interop. All I want to do is set the values on a chart in a word document so the graph can update to values in my app.

I've done the following (after importing Microsoft.Office.Interop.Graph.dll):

                InlineShape chartShape = WordDocument.InlineShapes[2];
                chartShape.Activate(); // for opening Chart in edit mode

                // Convert the InlineShape into Chart type which is a part of Microsoft.Office.Interop.Graph
                Microsoft.Office.Interop.Graph.Chart oChart = (Microsoft.Office.Interop.Graph.Chart) chartShape.OLEFormat.Object;
                Microsoft.Office.Interop.Graph.DataSheet dataSheet = oChart.Application.DataSheet;

                dataSheet.Cells[1, 1] = 10;

Firstly it throws a COMException (That metod is not available on that object) on the Activate() method. If I hope over that, the actual OLEFormat.Object throws an InvalidCastException (the specified cast is not valid).

Anyhow managed to get something like this to work?

A: 

Is your InlineShape a "MSGraph.Chart.8"?

Do not use the .Activate(), you need to make sure the OLE object is in a running state (I think this is why you get the invalid cast).

Microsoft.Office.Interop.Word.InlineShape chartShape = aDoc.InlineShapes[1];
if (chartShape.OLEFormat.ProgID == "MSGraph.Chart.8")
{
    object verb = Microsoft.Office.Interop.Word.WdOLEVerb.wdOLEVerbHide;
    chartShape.OLEFormat.DoVerb(ref verb);
    Graph.Chart oChart = (Graph.Chart)chartShape.OLEFormat.Object;
    Graph.DataSheet dataSheet = oChart.Application.DataSheet;
    dataSheet.Cells[1, 1] = 10;
}
Mark