Range Object Array in Word? RRS feed

  • Question

  • Some of my basic questions were asked in this post from years ago, but my basic lack of understanding prevails:

    Cindy mentioned going to bookmarks rather than ranges, and I may end up going there. However, I have started using Range Object variables with some success. What I don't under stand is the following:

    1. Will a Range Object variable, basically defined as the equivalent of a cursor location, persist when text, paragraph, page breaks, etc. are inserted before it. This is how I have defined my Range variables associated with a single photo caption (paragraph) so far.

    Dim capStart As Word.Range
    Dim capEnd As Word.Range
    Set capStart = Selection.Range
    Selection.EndOf Unit:=wdParagraph, Extend:=wdMove
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Set capEnd = Selection.Range

    2. If the range objects persist (e.g., move with the text and behave like bookmarks), can they be stored in an array for reference? My project builds an array that contains formatting and spacing constants for each photo caption (file name, vertical, horizontal, offsets, etc.). At the same time I run through the photo captions (could be hundreds of them) to build the array, can I store the Range (paragraph start) of each photo caption in the same or a different array?

    Thanks for any advice you may have.

    David Strickland


    • Edited by GrouseHiker Sunday, January 26, 2014 11:32 PM
    Sunday, January 26, 2014 11:31 PM


  • Hi David

    I believe my reply marked as "The Answer" in the discussion to which you refer covers point 1. If your users use the Backspace key in the wrong place, the Range object will "jump". This also pertains to a Bookmark object.

    Ranges can be stored in arrays, but the information won't be "permanent" in nature. Once the document is closed or your code goes out of scope it will no longer pertain.

    <<can I store the Range (paragraph start)>>

    You want to be very careful with storing only the Starting point, and you certainly do not want to do it with the Range.Start property - that will NOT remain the same if the user edits.

    Most stable would be to put these "target points" into Content Controls. These are stable AND persistent.

    If the information you mention should be persisted (rather than rebuilt each time the document is opened) it would probably even make sense to write it to a CustomXMLPart in the document, rather than holding it in a volatile Array (or other construct).

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by GrouseHiker Monday, January 27, 2014 6:51 PM
    Monday, January 27, 2014 5:35 PM