Graph.Chart interop in Word

痞子三分冷 提交于 2019-12-12 05:31:21

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/3555145/graph-chart-interop-in-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!