Range with 2 cells only has one

D

David Mann

I have had some funny results from a Word routine, which boils down to
having a range with cells.count=2, but only one cell. Can anyone explain
this?

1) Open Word and create a table with 2 rows and column.
Put 'a' in the top cell and 'b' in the bottom cell.

2) Open the immediate window and set a range to include both cells:

set rng=documents(1).Range(documents(1).Tables(1).Cell(1,1).Range.Start,
documents(1).Tables(1).Cell(2,1).Range.End)

3) Now examine the range:

?rng.cells.count returns 2

so it really does have two cells.

4) ?rng.cells(1).range returns 'a'

But, ?rng.cells(2).range does not return 'b',
in fact it fails, as does ?rng.cells(0).range.

"Run-time error '5941': The requested member of the collection does not
exist."

So how can I reference the second cell?

David
 
P

Perry

Why don't you use the column property as already specified in the
table object. You don't need to redefine a range based on the
cell specifics, instead utilize the column property to counter a Cells
collection
like examplified in below immediate window sequence.

set c = activedocument.Tables(1).columns(1)
? c.cells(1).range
A

? c.cells(2).range
B

in fact it fails, as does ?rng.cells(0).range.
The Cells collection is 1 based.

Krgrds,
Perry
 
D

David Mann

Perry said:
Why don't you use the column property as already specified in the
table object. You don't need to redefine a range based on the
cell specifics, instead utilize the column property to counter a Cells
collection
like examplified in below immediate window sequence.

set c = activedocument.Tables(1).columns(1)
? c.cells(1).range
A

? c.cells(2).range
B


The Cells collection is 1 based.

Krgrds,
Perry

Thanks for the work-around.

It seems strange that we can reference all the cells in a column(1).cells
collection by cell(index), but cannot reference the cells in the range.cells
collection in the same way.

What is worse is that once the table becomes more complicated, i.e. with
split cells, it is no longer possible to reference individual columns or
rows.

This is what happens:
1)Add a third row to the tale and split the new cell into two columns.
2) Try to reference column(1) of the table: we get "Cannot access
individual columns in the collection because the table has mixed cell
widths."

Fortunately, in this cases, referencing individual cells by
range.cells(index) appears to work...
1) set rng=documents(1).Range(documents(1).Tables(1).Cell(1,1).Range.Start,
documents(1).Tables(1).Cell(3,2).Range.End)
2) rng.cells(index).range now gives the contents of each of the four cells!

Just another quirk of the Word object model I suppose.

David
 

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