none
Getting the position of cross reference items? (the range of them?) RRS feed

  • Question

  • I'm trying to locate where in the Word document cross-reference items are relative to the cursor. I believe that the position is represented by Range in the API. Now, I get all CrossReferenceItems via the corresponding function, no problem. But it returns a bunch of strings. Is there a function to retrieve the range from those strings? (I'd like to know where they are relative to the cursor's current position.)

     

    Thanks.

    -Walter

    Wednesday, September 28, 2011 2:21 AM

Answers

  • Hi Walter

    <<I haven't been able to get the current range of the cursor.>>

    WordApp.Selection.Range

    <<I mean the location of them inside the document, such as their index if a document were a linear string of characters. I assume that's what the range is for.>>

    Every Range object has a Start and an End property that return what you think you want, but...

    The "index" in the "string" of characters won't mean much, as there can be things in that "string" that aren't always taken into account. It's not a reliable or a stable way to work in Word. If you could tell us more about what you want to actually DO with it we could probably be of more help.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Thursday, October 6, 2011 8:39 AM
    Saturday, October 1, 2011 8:02 AM
    Moderator

All replies

  • Hi Walter

    There's no function built into the UI that's going to give you a Range.

    The GetCrossReferences method returns an array of strings. As the Help says, it gives you what you see in the Insert Cross Reference dialog box, which is a list of items that can be cross-referenced. So the method name is somewhat misleading as it doesn't return the cross references available in the document, but a list of things that could be cross-referenced.

    As far as I know, there's no way to get the cross-references themselves directly. Instead, you'd need to loop the fields in the document, looking for the field type that makes the kind of cross reference you're interested (usually REF fields).

    For each fld in Document.Fields
      If fld.Type = Word.WdFieldType.wdFieldRef Then
        'Check fld.Code for relevant text string
      End If
    Next


    Cindy Meister, VSTO/Word MVP
    Wednesday, September 28, 2011 9:58 AM
    Moderator
  • Thanks for the answer, Cindy.

     

    If I loop through the fields, can I determine where they are in the document? (Much like I need to determine where the current location of the textcursor is in the document.)

     

    -Walter

    Wednesday, September 28, 2011 7:04 PM
  • Hi Walter

    Could you be a bit more specific about what you mean by "where they are in the document"?

    A Field.Code and/or Field.Result property returns a Range object. So you always have a Range. But I don't know what you mean by "where they are in the document".


    Cindy Meister, VSTO/Word MVP
    Thursday, September 29, 2011 5:53 AM
    Moderator
  • I mean the location of them inside the document, such as their index if a document were a linear string of characters. I assume that's what the range is for.

     

    I haven't been able to get the current range of the cursor. ActiveDocument.Range() seems to be a function to generate a range object, not return the current range.

     

    -Walter

    Saturday, October 1, 2011 3:38 AM
  • Hi Walter

    <<I haven't been able to get the current range of the cursor.>>

    WordApp.Selection.Range

    <<I mean the location of them inside the document, such as their index if a document were a linear string of characters. I assume that's what the range is for.>>

    Every Range object has a Start and an End property that return what you think you want, but...

    The "index" in the "string" of characters won't mean much, as there can be things in that "string" that aren't always taken into account. It's not a reliable or a stable way to work in Word. If you could tell us more about what you want to actually DO with it we could probably be of more help.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Thursday, October 6, 2011 8:39 AM
    Saturday, October 1, 2011 8:02 AM
    Moderator
  • Hi Walter,

    You can define the beginning and end points of a range using the character position numbers, or use the Start and End properties with objects such as Selection, Bookmark, or Range. The following example creates a Range object beginning at the start of the second paragraph and ending after the third paragraph.

    Set myDoc = ActiveDocument
    Set myRange = myDoc.Range(Start:=myDoc.Paragraphs(2).Range.Start, _
        End:=myDoc.Paragraphs(3).Range.End)

    For additional information and examples, see

    http://msdn.microsoft.com/en-us/library/aa264167(v=office.10).aspx (Working with Range objects)

    If you have any concerns, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 6, 2011 8:42 AM