Solved

Macro creating a chart from table doesn't work

Posted on 2006-06-19
8
519 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
ID: 16940501
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
ID: 16942242
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
ID: 16948512
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:mariej
ID: 16951078
GBahri - you're awesome!  That works beautifully!

Marie
0
 
LVL 7

Expert Comment

by:gbahri
ID: 16956982
Thanks once again
0
 

Author Comment

by:mariej
ID: 16992985
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
ID: 16995129
WOOHOO! I answered that one myself!

Have a good day everyone!

MarieJ
0
 
LVL 7

Expert Comment

by:gbahri
ID: 16998347
Thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

816 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

13 Experts available now in Live!

Get 1:1 Help Now