Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Macro to create Bar charts

Posted on 2016-09-20
4
Medium Priority
?
128 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 30

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 30

Accepted Solution

by:
Randy Downs earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
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…

610 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