none
Hyperlink and position in text. RRS feed

  • Question

  • Hello again,

    I have an issue with position after getting text from document. When there is a hyperlink - positions in Text fetched from Document.Range() doesn't correspond to positions fetched by ActiveDocument.Range(x, x). I've tried to get it with ActiveWindow.View.ShowFieldCodes = false; but that doesn't help me either.

    For example:

    I have string "Raz dwa trzy", i'm setting the hyperlink with "www" to "dwa", so with ShowFieldCodes i'm getting:

     "Raz  HYPERLINK \"www\"  trzy\r"

    I'm checking the position of "trzy" and ...

    doc.Words[8].Text
    "trzy"
    doc.Words[8].Start
    29

    it's 29 ... but 29 in current string is the end of the string:

    doc.Text.Substring(28)
    "\r"

    How i can get string from which i can get accurate position of string corresponding to the one from Range?

    Thursday, February 27, 2014 11:31 AM

Answers

  • I believe the fix for this must be well-known, but since you posted several hours ago and no-one has responded, perhaps it is not so obvious.

    The basic problem seems to come down to a longstanding design/implementation error in the Range object(or perhaps the Words collection) in Word.

    You can try to set which text Word looks at by setting the .TextRetrievalMode.IncludeFieldCodes property of the Range object you are working with to True or False.

    If you set it to False, then "trzy" is indeed word 3. Raz is 1, and dwa is 2.

    If you set it to True, then "trzy" is Word 8. Raz is 1, the opening field brace is 2, "HYPERLINK " is 3, the doube quote is 4, "www" is 5, the closing double quote is 6, and the closing field brace is 7

    Notice that in the first case you see the words in the field result, and in the second case you see the words in the field code.

    In both cases, the .Start of the range of the item in the .Words collection is the position of the Start of the character if you include both the field code and the result in the Range. But whether you set .TextRetrievalMode.IncludeFieldCodes to True or False, you do not get that a range that includes both the code and the result.

    So I think that you probably have to adjust the .Start by iterating through the fields in the range, and subtracting the lengths of the .result.text of each field. You may also find that some field types do not have a .Result. Nor do I know what happens if you try to deal with nested fields or perhaps work with a range that includes part of a field.

    All of which suggest to me that it might be worth trying to find another way to work with the range that you found.


    Peter Jamieson

    Thursday, February 27, 2014 8:14 PM