Solved

Macro creating a chart from table doesn't work

Posted on 2006-06-19
8
517 Views
Last Modified: 2008-01-09

I have a table in Word.  If I manually select the table and choose Insert, Object, Microsoft Chart, a chart is created beneath the table and the graph is based on the table I selected (and that info is in the datasheet).

However, if I create a macro to do that, it places the chart into the first table cell instead of beneath it and has the generic chart data (north, south, east west) instead of the data in my table.

The two commands in the macro are:

Selection.Tables(1).Select
Selection.InlineShapes.AddOLEObject ClassType:="MsGraph.Chart.8", Filename:="", LinkToFile:=False, DisplayAsIcon:=False

I recorded a macro and that's how I got the above two lines.

Can someone tell me how I can fix the macro so the result is the same as when I create the chart manually?

TIA MarieJ
0
Comment
Question by:mariej
  • 4
  • 4
8 Comments
 
LVL 7

Accepted Solution

by:
gbahri earned 250 total points
Comment Utility
Hi Mariej,

Try using "WordBasic.InsertChart" instead of second line, "Selection.InlineShapes.Add......", in your code.

"Selection.Tables(1).select" refers to first table in selection of Active Word Document. Word throws exception if table is not selected before running code. If you know there is only one table in document, replace first line of your code with:
Activedocument.tables(1).range.select

Hope, this solves your problem,

Thanks,

GBahri
0
 

Author Comment

by:mariej
Comment Utility
GBahri

That works GREAT!  Thank you!

Can you tell me if there is a way to insert a pie chart instead of the default bar chart by adding parameters to wordbasic.insertchart?

MarieJ
0
 
LVL 7

Expert Comment

by:gbahri
Comment Utility
Thanks for the grade.

Here is a workaround for setting pie chart as default. This cannot be configured through VBA. You've to follow these steps in Word document, so as to make pie chart your default chart.

1. Insert Chart from “Insert>Picture>Chart”
2. Right Click on the Chart and select “Chart Object>Edit”
3. Again right click on the Graphical representation inside chart and select “Chart Type”
4. Select Chart type of your choice (Pie in your case) & press button “Set as default chart” located on left-bottom corner of dialog. Select "Yes".
5. Press OK

Above steps will set Pie chart as your default chart.

Hope, this helps you a bit,

Thanks,

GBahri

0
 

Author Comment

by:mariej
Comment Utility
GBahri - you're awesome!  That works beautifully!

Marie
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Expert Comment

by:gbahri
Comment Utility
Thanks once again
0
 

Author Comment

by:mariej
Comment Utility
I need to include percentages in the pie chart that is created.  I can't record a macro that makes that change to the pie chart.  Is there a way using VBA code?

MarieJ
0
 

Author Comment

by:mariej
Comment Utility
WOOHOO! I answered that one myself!

Have a good day everyone!

MarieJ
0
 
LVL 7

Expert Comment

by:gbahri
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

763 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

8 Experts available now in Live!

Get 1:1 Help Now