Bug with cell merging in tables using Applescript

B

backroomboys

Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Once you have merged cells in a table in Word 2008 using Applescript, any further reference to the table's cells in the script produce an error.

As an example, open Word 2008 and run this script:
tell application "Microsoft Word"
        make new table at active document with properties {number of rows:4, number of columns:4}
        set mytable to table 1 of active document
        merge cell (cell 1 of row 1 of mytable) with (cell 1 of row 2 of mytable)
        merge cell (cell 1 of row 3 of mytable) with (cell 1 of row 4 of mytable)
end tell

The script inserts a 4x4 table, merges the left hand cells of rows 1 and 2 as it should, then fails with error "Microsoft Word got an error: The object you are trying to access does not exist" number -1728 from cell 1 of row 3 of table 1 of active document.

Any reference to a cell made in an Applescript after merging cells will produce the error. For example substituting "select (cell 3 of row 3 of mytable)" for the last line of the script produces the same error on running the script.

Is this a bug or am I missing something?
 
P

Peter Jamieson

AFAICS you cannot reference row objects within a table once you have
merged cells in a column and you cannot reference column objects once
you have merge two cells in a row.

You can however do this:

tell application "Microsoft Word"
make new table at active document with properties {number of rows:4,
number of columns:4}
set mytextobj to text object of table 1 of active document
merge cell (cell 1 of mytextobj) with (cell 5 of mytextobj)
merge cell (cell 9 of mytextobj) with (cell 13 of mytextobj)
end tell

but you will obviously need to keep track of how the cell numbers change
as you go along!

(FWIW I do not know whether you would get this problem with rows etc. in
Word 2004 + VBA or the Windows version of Word)

Peter Jamieson

http://tips.pjmsn.me.uk
 
J

John McGhie

I'm going to stick my neck way out on the chopping block here and suggest
that you are destroying the object you are trying to reference.

When you "set mytable to table 1..." you have set a reference to an area in
memory.

Then when you do the merge, the shape of the table changes in memory and the
object is destroyed.

I am guessing (guessing!!) that if you re-write the code without the
"set..." statement, it will work (very slowly!) because then you will have
to resolve cell 1 of row 1 of mytable table 1 of active document on each
iteration.

I seem to remember that Word has an issue with cells in rows in a table.
You may be better to address the cell directly (cell 11 of table 1).

Hope this helps

Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel
Once you have merged cells in a table in Word 2008 using Applescript, any
further reference to the table's cells in the script produce an error.

As an example, open Word 2008 and run this script:
tell application "Microsoft Word"
make new table at active document with properties {number of rows:4,
number of columns:4}
set mytable to table 1 of active document
merge cell (cell 1 of row 1 of mytable) with (cell 1 of row 2 of
mytable)
merge cell (cell 1 of row 3 of mytable) with (cell 1 of row 4 of
mytable)
end tell

The script inserts a 4x4 table, merges the left hand cells of rows 1 and 2 as
it should, then fails with error "Microsoft Word got an error: The object you
are trying to access does not exist" number -1728 from cell 1 of row 3 of
table 1 of active document.

Any reference to a cell made in an Applescript after merging cells will
produce the error. For example substituting "select (cell 3 of row 3 of
mytable)" for the last line of the script produces the same error on running
the script.

Is this a bug or am I missing something?

--

The email below is my business email -- Please do not email me about forum
matters unless I ask you to; or unless you intend to pay!

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410 | mailto:[email protected]
 
B

backroomboys

I tried Peter's script and the second merge was't quite right - it merged cells in rows 3 and 4 of column 2, rather than column 1. From what you both say, it looked like the first merge altered the numbering of cells below the merge, thereby causing the second merge to go wrong.

I reversed the order of the merges in the script, so that the first merge was at the bottom of the table and the second was at the top and achieved merging of the cells in the left-hand column as I wanted.

Thanks for solving this one so quickly! I'll use cell numbering and the bottom to top order of merging in future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top