Solved

Macro to create Bar charts

Posted on 2016-09-20
4
60 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now