Ranges with Fields RRS feed

  • Question

  • Hello,

        Could someone explain how the Range.End is determined when a Range contains one or more fields?  The text of the range is either the fieldcode or the displayed text depending on the View.ShowFieldCodes value.  However, the End property is not the Start property Value + Range.Text.Length (like it is when there is no field in the Range).  I thought I figured it out by including the start char, fieldCode value, separator char, text value, end char.  However, sometimes this is valid and other times it's off by a character (when I insert a hyperlink, for example) when manually calculating what I think the End position should be.  So, I just would like to understand how the End position is calculated for a Range with one or more Fields in that Range.  Any help is appreciated.


    Tuesday, February 10, 2015 4:02 PM

All replies

  • Hi Brad,

    Thank you for posting in the MSDN Forum.
    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.
    Sorry for any inconvenience and have a nice day!

    Regards & Fei

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 17, 2015 7:56 AM
  • Hi Brad,

    It appears that Word saves field code and the content in the range and you are shown just the text you wish to see while you toggle field data.

    > The value looks to be addition of Field Code  + Field content + 1 space

    > For eg. If the field code is "{ AUTHOR   \* MERGEFORMAT}" which is 27 characters and the content  be "" which has 20 characters and 1 space, will give you 48 as Selection.Range.End
    > The following VBA will give length if you wish to do this programmatically:

    Sub test()
    Dim iCnt As String
    iCnt = Selection.Range.End
    Dim FieldLength As Integer
    Length = 0
    For i = 1 To Selection.End
        FieldLength = FieldLength + 1
        'Debug.Print i & " :" & ActiveDocument.Range(i, i).Characters(1).Text & ": " & Asc(ActiveDocument.Range(i, i).Characters(1).Text)
    Debug.Print FieldLength
    End Sub

    Hope this helps!


    Thursday, March 19, 2015 9:53 PM