Introduction
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.
Background
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
Code:
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
chartShape.Activate()
'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)
oShape.ZOrder(Office.MsoZOrderCmd.msoSendBehindText)
Else
'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
chartShape.Delete()
End If
End If

