Microsoft Word Chart Formatting using VBA example

Microsoft Word Chart Formatting using VBA example

Chart Formatting

There are many objects involved to build a complete chart object like Plot Area, Legends, Title, Axis, Chart Area, Series etc. Each object has its own properties which help manipulating that specific object within chart. In this article we will try to learn formatting of a chart in Microsoft Word Document: Following is the basic chart which is created using below data source which we will take through this article:

Data Source

Format Plot Area

  1. Change the border line style
  2. make border color to white

Code example

Public Sub FormatChartArea()
    'Declare document object
    Dim oDocument As Document
    
    'Bind document reference
    Set oDocument = ActiveDocument
    
    'inline shape object
    Dim oInLineShape As InlineShape
    
    'Iterate each inline shape
    For Each oInLineShape In oDocument.InlineShapes
        'Checks for valid chart
        If oInLineShape.HasChart Then
            'Declare chart object
            Dim oChart As Chart
            Set oChart = oInLineShape.Chart
            
            'Format the chart Plot area
            oChart.ChartArea.Border.LineStyle = xlDash
            oChart.PlotArea.Border.LineStyle = xlDot
            oChart.PlotArea.Border.Color = vbWhite
            
            'Clean up memory
            Set oChart = Nothing
        End If
    Next oInLineShape
    
    'Clean up memory
    Set oInLineShape = Nothing
End Sub

Output

Other formatting

  1. Add data labels to the series
  2. Apply layout where Axis title will be enabled and legend moves to right
  3. Change chart type

Code example

Public Sub FormatOtherObjectsOfChartArea()
    'Declare document object
    Dim oDocument As Document
    
    'Bind document reference
    Set oDocument = ActiveDocument
    
    'inline shape object
    Dim oInLineShape As InlineShape
    
    'Iterate each inline shape
    For Each oInLineShape In oDocument.InlineShapes
        'Checks for valid chart
        If oInLineShape.HasChart Then
            'Declare chart object
            Dim oChart As Chart
            Set oChart = oInLineShape.Chart
            
            'Format the chart Plot area
            oChart.ChartArea.Border.LineStyle = xlDash
            oChart.PlotArea.Border.LineStyle = xlDot
            oChart.PlotArea.Border.Color = vbWhite
            'Add data labels
            oChart.ApplyDataLabels xlDataLabelsShowValue
            'Apply Axis titles
            oChart.ApplyLayout 1, xl3DSurface
            'Change chart type
            oChart.ChartType = xl3DAreaStacked
            'Clean up memory
            Set oChart = Nothing
        End If
    Next oInLineShape
    
    'Clean up memory
    Set oInLineShape = Nothing
End Sub

Output

Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.

Leave a Reply

Your email address will not be published. Required fields are marked *