1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Manipulating existing Chart Object in MS Word using .NET

Discussion in 'ASP.NET' started by naimish, Sep 18, 2009.

  1. naimish

    naimish New Member

    Jun 29, 2009
    Likes Received:
    Trophy Points:
    Software Engineer
    On Earth


    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
  2. shabbir

    shabbir Administrator Staff Member

    Jul 12, 2004
    Likes Received:
    Trophy Points:
  3. rasd123

    rasd123 Banned

    Nov 4, 2009
    Likes Received:
    Trophy Points:
    Valuable information.
  4. Larrabee

    Larrabee New Member

    Dec 5, 2009
    Likes Received:
    Trophy Points:
    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,


Share This Page