PDA

View Full Version : Solved: Incorrect Results with SetRange



MWE
09-02-2008, 05:03 PM
I am using Word2003. I am having trouble setting a range to some part of the active document. I wrote a test procedure to see if there might be something strange with my original code, but the test procedure returns the same incorrect results.Sub TextRangeExtract()
Dim I1 As Long
Dim I2 As Long
Dim wrdRange As Range

I1 = 10
I2 = 12
Set wrdRange = ActiveDocument.Range(I1, I2)
MsgBox wrdRange.Text & vbTab & Len(wrdRange.Text)

End Sub
In the above, one would expect that wrdRange contains the 10th, 11th and 12th characters in ActiveDocument. But it does not. Rather it contains the 11th and 12th characters. I also triedwrdRange.SetRange I1, I2 and got the same incorrect results. It does not seem to matter how large a difference there is between the start and end; the results is always that the first character is not included.

Per VBA Help:

SetRange Method
expression.SetRange(Start, End)
expression Required. An expression that returns a Range or Selection object.
Start Required Long. The starting character position of the range or selection.
End Required Long. The ending character position of the range or selection.


Can anyone explain what is going on here?

MWE
09-02-2008, 08:52 PM
After some further poking around, I have found an "explanation", but I do not like it (whine, whine, whine). Per VBA Help on Range stuff,

Character position values start at the beginning of the story, with the first value being 0 (zero). All characters are counted, including nonprinting characters. Hidden characters are counted even if they're not displayed.

So this is base zero issue and if I want to reference the 10th character in the range's text, that is position #9. That explains why the characters I do get are shifted; and that is understandable. But the following example, also from VBA Help, did not initially make sense:

SetRange Method Example (http://www.vbaexpress.com/forum/)

This example selects the first 10 characters in the document.
Selection.SetRange Start:=0, End:=10

If we start at position zero and end at position 10, that is 11 characters. As indicated in my first message, start and end are defined as:

SetRange MethodStart Required Long. The starting character position of the range or selection.
End Required Long. The ending character position of the range or selection.


Possible explanations:
the start is base zero and the end is base 1 :think:
"start" means "include this character" and "end" means "end before this character" :dunno that does not agree with the definition of "end"
"end" really means the ending character position, not the last character.In poking around MSDN, I found a blurb on the End property. The following example indicates that the end of a range or selection is really 1 character after the last character:

This example retrieves the ending position of the selection. This value is used to create a range so that a field can be inserted after the selection.
pos = Selection.End
Set myRange = ActiveDocument.Range(Start:=pos, End:=pos)

If you use instr to find strings in ranges and then setrange to isolate a substring for formating or to insert text or similar (as I am trying to do), this can be confusing.

TonyJollans
09-04-2008, 05:16 PM
You seem to be making heavy weather of this!

A 'position' itself has zero length - it is not a character, it is a position before or after a character, so there are two ways this could work:

Position (start or end) means before the character number whatever.
Position (start or end) means after the character number whatever.


You seem to want a mixture: start position to be before character number zero and end position to be after character number 10. Now that would be confusing :)

MWE
09-05-2008, 11:50 AM
You seem to be making heavy weather of this!

A 'position' itself has zero length - it is not a character, it is a position before or after a character, so there are two ways this could work:
Position (start or end) means before the character number whatever.
Position (start or end) means after the character number whatever.
You seem to want a mixture: start position to be before character number zero and end position to be after character number 10. Now that would be confusing :)heavy weather is probably right. I came to the same conclusion about "position" and am no longer having problems with that aspect of SetRange. But I am having other problems and I think that some of the trouble is that my version of Word2003 is not behaving itself.