How do I programmatically insert, or edit, a pie chart in a Word 2007 document, using vs 2010, c#?
I need to programmatically insert, or edit, a pie chart in a Word 2007 document, using vs 2010, c# ... everything I have found shows how to put these old ugly charts into a word doc. so I am now wondering if it is even possible to manipulate the newer and better looking charts.
This one shows how to do just what I want, only it's the old ugly charts.... http://msdn.microsoft.com/en-us/library/ms178766.aspx. It tells you to insert a OLE object, and it's the ancient msgraph.chart.8 stuff.
开发者_JAVA百科I've been able to do everything I need to do except use the newer style of charts.
Here's some of the code. I built a new pie chart and now how do I insert it into the Word document? My PieChart3D class is based on these http://code.msdn.microsoft.com/mschart
// here's my c#.net
private void CreateChart(string title, Microsoft.Office.Interop.Word.Application oWord, Microsoft.Office.Interop.Word.Document oDoc, ChartType chartType, Hashtable values)
{
PieChart3D chart1 = new PieChart3D(); // using System.Windows.Forms.DataVisualization.Charting
chart1.PieChart3D_Load(values);
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
Microsoft.Office.Interop.Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
// None of these work!!!!
wrdRng.InlineShapes.AddOLEControl(chart1);
wrdRng.InlineShapes.AddChart(chart1);
wrdRng.InlineShapes.AddOLEObject(chart1);
return;
}
// done with code
It seems to me I only need the last step of injecting it into the document. What am I missing?
You should probably add the missing parameters in your 'AddOLEControl()' call just for good measure.
How to insert an excel chart into Word using AddOLEObject
http://support.microsoft.com/kb/316384
None of these options will actually work for you. AddChart is used to create a chart that Word already knows about - you have to use one of the known chart types. AddOLEControl and AddOLEObject both require your Chart class to be registered for COM. When you call the method, it creates a new instance of the class and adds it to the form. You can't use these methods to add an existing chart that you created in C#.
I think your best bet is to save your chart to a file using the SaveImage method, then add it to your WORD document using the AddPicture method. You won't be able to modify the chart in Word, you'll have to delete it and re-add it, but at least you can display the chart you created.
I found a sample code.
http://www.codeproject.com/Articles/188909/Updating-Charts-in-Word-Document-using-OpenXML
In this code create a word cocument first. You can set its style in the word document. Then you can update the chart with just one procedure call. hope it helps.
精彩评论