Solved

Macro to create Bar charts

Posted on 2016-09-20
4
96 Views
Last Modified: 2016-09-28
Hello Experts,

I need some sample to read a table from a word file and generate a bar charts like below:

Chart output
And the data that will be on a word file will be as like below:

Chart Data on Word file
Is there any sample that will be help me in achieving this using Macro code. So that when ever the word file will be generated and someone opens it , the macro code will generate the chart and hide the entire table.

My Sample file will be as like the attached one.

SampleWordFile.odt

In the attached file the "Overall Risk Rating Trend" is the Dynamic chart that need to be generated using the table "Chart Data" at the end of the file.

Once the chart is generated the table should be hidden or removed.

Thanks,
Swadhin
0
Comment
Question by:Swadhin Ray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Randy Downs
ID: 41807978
There are some pretty good samples here

This article contains two samples: a basic chart in Word (WordChart.docm), and a pair of files (ExcelWithChart.xlsm and WordChartLinkedToExcel.docm) that show how to have an interactive Word document that uses a chart created in Excel. You can download the code samples from the MSDN Code Gallery.
0
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 41812838
Say for example I build a html table in a word file with the chart data , and can we read the data from the table and build an chart dynamically.
0
 
LVL 29

Accepted Solution

by:
Randy Downs earned 500 total points
ID: 41812872
this is based on the original article I quoted.

reference to the Microsoft Excel 14.0 Object Library

Sub MakeChartFromTable()

Dim myTable As Table
Dim salesChart As Chart
Dim chartWorkSheet As Excel.Worksheet
Dim x As Integer
Dim RowCount As Integer
Dim ColumnCount As Integer
Dim LastColumn As String

For Each myTable In ActiveDocument.Tables
    myTable.Range.Copy
    'Create Chart
    Set salesChart = ActiveDocument.Shapes.AddChart.Chart
    Set chartWorkSheet = salesChart.ChartData.Workbook.Worksheets(1)
    'Determine size of table
    RowCount = myTable.Rows.Count
    ColumnCount = myTable.Columns.Count
    'Determine spreadsheet column letter for last column of table
    If ColumnCount < 26 Then
        LastColumn = Chr(64 + ColumnCount)
    Else
        LastColumn = Chr(Int(ColumnCount / 26) + 64) & Chr((ColumnCount Mod 26) + 64)
    End If
    'Resize chart data area to table size and paste table data
    With chartWorkSheet
        .ListObjects("Table1").DataBodyRange.Delete
        .ListObjects("Table1").Resize chartWorkSheet.Range("A1:" & LastColumn & RowCount)
        .Range("A1:" & LastColumn & RowCount).Select
        .Paste
    End With
    salesChart.ChartData.Workbook.Close
Next

End Sub

Open in new window

0
 
LVL 16

Author Closing Comment

by:Swadhin Ray
ID: 41819713
Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Outlook Free & Paid Tools
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question