Solved

Macro creating a chart from table doesn't work

Posted on 2006-06-19
8
520 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

839 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