Manipulating existing Chart Object in MS Word using .NET

naimish's Avatar author of Manipulating existing Chart Object in MS Word using .NET
This is an article on Manipulating existing Chart Object in MS Word using .NET in ASP.NET.
Rated 5.00 By 1 users


The functioned mentioned manipulates the already added bar graph(Chart object) using VB.NET(front end).

One can change the value of the bar graphs, the properties of the chart at runtime. Some of the properties are accessed in the function.

Validation can also been performed.


The chart object is captured by using InlineShapes property of the Word Document instance. One need to add reference to Microsoft.Office.Interop.Graph dll to the solution before using the function.

InlineShapes object is converted to Chart object before manipulating. There is a Datasheet for each bar graph in MS Word. To access that we declare a DataSheet variable. Validations can be used depending on the requirement.

DataSheets.Cells property is used to assign values to the bar graphs at runtime. Other properties can also be used depending on the requirement.

Here, for example,oShape.Left is used for aligning the chart to the left side of the page.

Similarly, the object can also be deleted if the object fails to pass the validations.

The code

Code: VB.NET

          Private Sub PopulateGraph(ByVal oDoc As Word11.Document, ByVal graphValue1 As Double, ByVal graphValue2 As Double)
            'oDoc is instance of Word document which is to be accessed.
            'Checks if there is any chart object available
            If oDoc.InlineShapes.Count > 0 Then
           'To access the chart object in the template using InlineShapes
            Dim chartShape As Word.InlineShape = oDoc.InlineShapes.Item(0)
            'For opening Chart in edit mode
            'Convert the InlineShape into Chart type which is a part of Microsoft.Office.Interop.Graph
            Dim oChart As Graph.Chart = chartShape.OLEFormat.Object
            'To access the datasheet of the chart
            Dim dataSheet As Graph.DataSheet = oChart.Application.DataSheet
            'If condition to check if the data values for the graphs is null or empty or zero.
            'Goes inside If condition if the values are neither null or empty or zero
            If graphValue1 > 0.0 And graphValue2 > 0.0 And graphValue1 > graphValue2 Then
                'Assigning values to the cells in datasheet for first bar
                dataSheet.Cells(2, 2) = graphValue1
                'Assigning values to the cells in datasheet for second bar
                dataSheet.Cells(3, 3) = graphValue2
                'To Convert the InlineShape to Shape for doing other operations
                Dim oShape As Word.Shape = chartShape.ConvertToShape()
                'For aligning to object to the left hand side
                oShape.Left = CType(Word.WdShapePosition.wdShapeLeft, Single)
                'To place the picture behind the text(Behind text property)
                'If either of the data values is null or empty or zero or current cost is less than proposed cost then the chart is deleted
            End If
        End If
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination for Article of the month - Sep 2009 Started. Nominate this article.
rasd123's Avatar
Valuable information.
Larrabee's Avatar, Join Date: Dec 2009
Newbie Member
Thank you for the interesting post.

I have tried to integrate your code into a C# project of mine but unfortunately I keep getting COM Errors.


foreach (InlineShape inlineShape in _wordApp.ActiveDocument.InlineShapes)
if (inlineShape.Type == WdInlineShapeType.wdInlineShapeEmbeddedOLEObject)

Chart chart = (Chart)inlineShape.OLEFormat.Object;
DataSheet datasheet = (chart as Chart).Application.DataSheet;
datasheet.Cells[0, 0] = "1";


Where _wordApp is a reference to an open Word Application which has an empty Document in it with a couple of charts.

This code fails on the following line:


with the following COM-Exception: HRESULT: 0x800A1704.

After removing the line, it fails on the next line:

Chart chart = (Chart)inlineShape.OLEFormat.Object;

with the following COM-Exception: HRESULT: 0x800401FD (CO_E_OBJNOTCONNECTED))

Does anyone have an idea on how to fix this?

This might not be the proper place to ask this kind of question, but the documentation concerning the Microsoft.Office.Interop.Graph.dll is limited to say the least.

Basically what I am trying to achieve is manipulating the data of an already existing chart in a Word-Document at runtime. There is another type of chart (Insert -> Picture -> Chart), which is even less documented and doesn't seem to even support OLEFormat.Object.

Thanks for the help,