Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Manipulating existing Chart Object in MS Word using .NET (http://www.go4expert.com/articles/manipulating-existing-chart-object-ms-t19471/)

naimish 18Sep2009 10:16

Manipulating existing Chart Object in MS Word using .NET


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 3Oct2009 17:06

Re: Manipulating existing Chart Object in MS Word using .NET
Nomination for Article of the month - Sep 2009 Started. Nominate this article.

rasd123 17Nov2009 05:44

Re: Manipulating existing Chart Object in MS Word using .NET
Valuable information.

Larrabee 5Dec2009 21:37

Re: Manipulating existing Chart Object in MS Word using .NET
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,


All times are GMT +5.5. The time now is 03:46.