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
Solved

Macro to create Bar charts

Posted on 2016-09-20
4
90 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
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 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