Solved

Macro creating a chart from table doesn't work

Posted on 2006-06-19
8
521 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
[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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

733 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