Question : Macro creating a chart from table doesn't work


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

Answer : Macro creating a chart from table doesn't work

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
Random Solutions  
 
programming4us programming4us