?
Solved

Macro creating a chart from table doesn't work

Posted on 2006-06-19
8
Medium Priority
?
523 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 1000 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
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses

752 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