Range(word) not listing all Fields

D

David Thielen

Hi;

Take a look at the file in
http://www.windwardreports.com/temp/BadFieldCount.zip If you create a
Range object with a Start=0 and End=498, the Fields.Count member ==
3(which is correct).

But if you set Start=0 and End=649, Fields.Count==1 -> it has dropped
the first 3. This is in Word 2007.

What's going on here? And what do I do to work around this?

thanks - dave

david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
J

Ji Zhou

Hello Dave,

I test with the document you provide. I can reproduce the issue. Based on my
investigation, I think I can explain why it behaves this way. But I cannot
tell a workaround because I am not clear with the objective here.

Here are my steps of researches, as well as explanations.

Codes:
Sub Test1()
ActiveDocument.Range(498, 498).Select
End Sub

Sub Test2()
ActiveDocument.Range(0, 498).Select
End Sub

Sub Test3()
ActiveDocument.Range(649, 649).Select
End Sub

Sub Test4()
ActiveDocument.Range(0, 649).Select
End Sub

If we run Test1, we can see the 498 point is at the end of the last cell in
the second row of the table.
If we run Test3, we can see the 649 point is at the end of the second cell
in the third row of the table.

Then,
if we run Test2, we can find the selection contains all columns in the 1,2
row, because the end point is at the last column.
if we run Test4, we can find the selection contains only the first and the
second column in 1,2,3 row, because the end point is at the second column.

From the selection, we can see clearly, Range(0,649) indeed does not include
that three Fields. So what the codes return should be correct. So the root
cause of the issue is, when we call Range() to locate a range inside a
table, it uses the column-preferable selection way.

To describe it more clearly, imagine the following table which has 3 columns
and 2 rows. And all cells are empty.

C11 C12 C13
C21 C22 C23

Then,

Range(0,1) contains the C11
Range(0,2) contains the C11, C12
Range(0,3) contains the first row
Range(0,4) contains the first row and the Enter character
Range(0,5) contains the C11, C21 (the first column)
....

So Range(0,5) will not include any fields in the C12, and C13.


Best regards,
Ji Zhou - MSFT
Microsoft Online Community Support
 
T

Tony Jollans

This is, shall we say, an anomaly, :(

You can see whats happening in the UI, without using any code - you can only
select rectangular areas. Your Range to 649 would include all the cells from
one row and some from the next, but it is unselectable - it could never be a
Selection.

It can, however, be a Range and, indeed, is a Range, but the Fields
collection is being reported as though it were the Selection - this is, IMO,
a bug.

If you know you are in a Table, you can work around it by walking the
Range.Cells collection - all the cells are in the Cells collection, and the
fields are within the Cells, so you build your own Collection ...

Set r = ActiveDocument.Range(0, 649)
For Each c In r.Cells
For Each f In c.Range.Fields
RangeFields.Add f
Next f, c

RangeFields can now be used in the way you wanted to, and should be able to,
use Range.Fields - except for things like its Parent.
 
D

David Thielen

Bingo - your and Tony's answers give me what I need.

thanks to both of you - dave


Hello Dave,

I test with the document you provide. I can reproduce the issue. Based on my
investigation, I think I can explain why it behaves this way. But I cannot
tell a workaround because I am not clear with the objective here.

Here are my steps of researches, as well as explanations.

Codes:
Sub Test1()
ActiveDocument.Range(498, 498).Select
End Sub

Sub Test2()
ActiveDocument.Range(0, 498).Select
End Sub

Sub Test3()
ActiveDocument.Range(649, 649).Select
End Sub

Sub Test4()
ActiveDocument.Range(0, 649).Select
End Sub

If we run Test1, we can see the 498 point is at the end of the last cell in
the second row of the table.
If we run Test3, we can see the 649 point is at the end of the second cell
in the third row of the table.

Then,
if we run Test2, we can find the selection contains all columns in the 1,2
row, because the end point is at the last column.
if we run Test4, we can find the selection contains only the first and the
second column in 1,2,3 row, because the end point is at the second column.

From the selection, we can see clearly, Range(0,649) indeed does not include
that three Fields. So what the codes return should be correct. So the root
cause of the issue is, when we call Range() to locate a range inside a
table, it uses the column-preferable selection way.

To describe it more clearly, imagine the following table which has 3 columns
and 2 rows. And all cells are empty.

C11 C12 C13
C21 C22 C23

Then,

Range(0,1) contains the C11
Range(0,2) contains the C11, C12
Range(0,3) contains the first row
Range(0,4) contains the first row and the Enter character
Range(0,5) contains the C11, C21 (the first column)
...

So Range(0,5) will not include any fields in the C12, and C13.


Best regards,
Ji Zhou - MSFT
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 

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