PDA

View Full Version : VBA Word 2003



user2012
09-12-2012, 02:54 AM
Hi,

I need to use macro to generate a graph in word 2003. The data will come from a dynamic table (the number of rows is not fixed).
I have done it in word 2007, however the same script does not work for word 2003 even though backwards compatibility has been checked.

Any guide or reference will be highly appreciated.

Many Thanks.

Frosty
09-12-2012, 05:58 AM
Maybe you should post your 2007 code that doesn't work in 2003?

user2012
09-12-2012, 08:37 AM
The code that works in 2007 is below. when the same is compiled in 2003, the error that AddChart, Chart is not recognised.

Sub ChartFromTable()
Dim myTable As Table
Dim salesChart As Chart
Dim chartWorkSheet As Excel.Worksheet
Dim x As Integer
Dim RowCount As Integer
Dim ColumnCount As Integer
Dim LastColumn As String
For Each myTable In ActiveDocument.Tables
myTable.Range.Copy 'Create Chart

Selection.InsertBreak Type:=wdSectionBreakNextPage
Set salesChart = ActiveDocument.Shapes.AddChart.Chart

Set chartWorkSheet = salesChart.ChartData.Workbook.Worksheets(1)
'Determine size of table
RowCount = myTable.Rows.Count
ColumnCount = myTable.Columns.Count
'Determine spreadsheet column letter for last column of table
If ColumnCount < 26 Then
LastColumn = Chr(64 + ColumnCount)
Else
LastColumn = Chr(Int(ColumnCount / 26) + 64) & Chr((ColumnCount Mod 26) + 64)
End If
'Resize chart data area to table size and paste table data
With chartWorkSheet
.ListObjects("Table1").DataBodyRange.Delete
.ListObjects("Table1").Resize chartWorkSheet.Range("A1:" & LastColumn & RowCount)
.Range("A1:" & LastColumn & RowCount).Select
.Paste
End With
salesChart.ChartData.Workbook.Close
Next
End Sub
Sub AutoOpen() 'runs when document is opened
'This name "AutoOpen" is a Visual Basic reserved name for launching
'code when a Word document is opened.
'AutoOpen works in Word 'Auto_Open works in Excel
ChartFromTable
End Sub


Kindly advise.

Many Thanks

Frosty
09-12-2012, 09:23 AM
You can use the VBA tags to post more readable code.

Couple of notes:

Are you using Option Explicit? Because you have a bigger problem than the .AddChart method not being available in 2003. You don't even have a Chart object available, at least not in the same way. Yes, you can embed excel charts, but you can't give them data just by selecting a word table and saying "create a chart from this table"

So this entire "ChartFromTable" would need to be rethought for 2003, because 2003 simply doesn't have the same charting options, in terms of having the data in Word.

Is that something you want to tackle? Because you're talking about trying to get 2003 to provide similar functionality to 2007. I'm sure it's possible, but it will not be easy-- and you're going to have to do a lot of work.

It might be simpler to simply purchase Word 2010.

user2012
09-12-2012, 08:58 PM
Thanks for your feedback Frosty.
Actually I need to embed this macro in a CRM functionality that uses Word 2003, and thats the reason I am trying to do it using an old version of word.

Frosty
09-12-2012, 10:19 PM
I think you have a lot of work to do ;)

You'll need to set up a word document with representative dummy data, and a manually created embedded excel chart with the same dummy data, formatted the way you would like it to be formatted. It can be a single word document with 2 pages, the first page being the "before" and the second page being the "after"... But it has to be a 2003 doc.

Do that, and then we can see how many people can help do some pieces. I haven't worked heavily with embedded objects for some time, since they were always buggy and clunky to begin with. But there is probably a way to get close.

Post your samples.