• Simple VBA Range Question (Office/VBA 2003)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Simple VBA Range Question (Office/VBA 2003)

    Author
    Topic
    #448390

    Hi, all. Can someone explain to me (in clearer terms that found in the Help files) why I can use the Start and End properties when setting a range relative to the ActiveDocument.Range object, but I cannot do the same with an individual paragraph? In other words, why does it seem I must use two lines of code (Set myRange = overlylarge_range then myRange.SetRange fromHere, toHere) to set the desired range to part of a paragraph’s range, but only one line would be needed to do the same with the ActivedDocument.Range (e.g. Set myRange = ActiveDocument.Range(fromHere, toHere).

    I realize I may simply be missing something, but I’d love to know what it might be!

    Thanks in advance…

    Viewing 0 reply threads
    Author
    Replies
    • #1096372

      Range is – confusingly – both an property and a method in the Word object model (and an object, of course).

      Range(start, end) is a method of the Document object, but not of the Selection or Paragraph object. Both the Range method of the Document object and the SetRange method of the Range and Selection objects use start and end as absolute positions within the document, never as relative to another range.

      Example:

      Dim rng As Range
      Set rng = ActiveDocument.Range(100, 120) ‘ rng extends from character 101 to 120 in the document
      rng.SetRange 10, 15 ‘ rng extends from character 11 to 15 in the document

      The last instruction moves rng completely outside the original range.

    Viewing 0 reply threads
    Reply To: Simple VBA Range Question (Office/VBA 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: