PDA

View Full Version : Range Object



qazpl
11-21-2006, 06:24 AM
I find the range object sometimes confusing. While it increments one number up for each character when it comes to fields it will increment up 50 to 100 number. For example crossReferences or Table of Contents etc...
To test this select a field and run Macro


Sub CheckRange()
msgbox selection.Range.Start & " - " & selection.Range.End
End Sub


While I understand that the reason for this is the field codes it does make it impossible to use regex to search document with fields because the numbers of location in String dont match to the location of the range


For Each RegM In RegC
Set MatchArr(i) = ActiveDocument.Range(RegM.FirstIndex, _
RegM.FirstIndex + RegM.Length)
Next


The firstIndex and Range.First don't match when fields are in Document

fumei
11-21-2006, 07:01 AM
it does make it impossible to use regex to search document with fieldsThis is not quite accurate. It makes it difficult to use RegEx where you use the Range of the matches. RegEx is working. It is Word that makes these...hmmmm....variable (and weirdly inconsistent!) range dimensions for fields.

For another bizarre example:

1. make a new document
2. put in ONE text formfield (no text before it, just ONE formfield)
3. without protecting for forms, select it and run your CheckRange

Return: 13 - 18 ???Huh?

4. protect the form, type in one character, say "d".
5. tab back so the single character formfield is selected, and run CheckRange

Return: 13 - 14

Ok, maybe th elength is sort of reasonable, but why 13???

The document has NOTHING but a single formfield, with "d" in it. Yet selected, the range is 13 - 14.

Go figure.

I am afraid you are stuck. As far as I know there is nothing you can do about this.

It is sort of similar to a texbox in a document. The textbox may contain 400 characters of text, but they are never considered within any use of Range. CheckRange returns an increase of only 1 between the location immediately before the textbox, and the position immediately after. It is an InlineShape, and by definition, is one character in length.

fumei
11-21-2006, 07:06 AM
BTW: in the formfield example above, it is impossible to determine what is happening in those 0 -12 characters. They are somehow in a black hole. Even though the formfield range is 13 - ..., even though there nothing whatsoever before it....0 - 12 are...somewhere...floating off into Word-land. Oh, not quite accurate. 0 exists fine. The cursor at 0 returns 0 - 0. It is 1 - 12 that seem to vanish.

It would not be so bad it it STAYED the same. But as you have found out...the darn thing can change!

qazpl
11-22-2006, 09:10 PM
Is there anything else besides a field that effects the range. because I want to make 2 options in macro if no range.fields.count = 0 then regex
else ms words find. because I find regex option much faster
thanks
qazpl

fumei
11-22-2006, 10:15 PM
Not that I know of.

TonyJollans
11-23-2006, 12:06 PM
I don't know what the values are that you are getting back from the RegEx and am not familiar enough with it to knock up a quick test (and have enough other problems with Vista and Office 2007 at the moment!).

The Document Content (Range) includes details of the Word Field and its contents, only one of which is displayed at a time via the Alt+F9 toggle (or TextRetrievalMode setting in VBA) but both of which have to be stored.

I suspect what you need to do is use your RegEx numbers as start and length of a Mid$ on the Range.Text rather than as start and end positions within the Range itself.

If it helps at all, in Gerry's example the full Range is:

{ FORMTEXT *| }

The braces are the field delimiters (0h13 and 0h15 actually)
The pipe is an internal delimiter (0h14) between the field code and the field value.
The spaces between the pipe and the right brace are the default value of a form text field.
The asterisk is a placeholder (0h1) for the field.

TonyJollans
11-23-2006, 12:15 PM
... and to answer your later question, there may be other things that cause similar problems, but these spring to mind:

Hidden text.
Table End of cell and End of Row marks.
Unicode surrogate pairs (if you are likely to encounter them).