Results 1 to 7 of 7
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Best practice: Selection vs. Range (Word 2002 SP-2)

    In writing Word macros, there are clearly countless situations where you're going to work with a contiguous portion of a document and you can either select the relevant text and work with the selection or define a range and work with the range. Assuming the substance of the particular macro (and what the user may be doing in its wake) doesn't particularly favor one method over the other, and assuming the macro-writer doesn't mind if the range-method code is a little more involved, are there other reasons why it's generally better to use one method rather than the other?

    If selecting text involves manipulating the application window and defining ranges doesn't, I'm guessing code that works with ranges may tend to run "cleaner" (so to speak), but I don't really know what I'm talking about in this area (which was probably clear enough already).
    <img src=/S/anigrin.gif border=0 alt=anigrin width=19 height=19>

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Best practice: Selection vs. Range (Word 2002 SP-2)

    In general, you should use ranges instead of selection. Changing the selection all the time slows down macro execution. The same holds for progamming in Excel.

    There are exceptions, however. For example, if you want to loop through the cells of a table column, selecting the column is much faster than using ranges - see Maximising the performance of tables on the Word MVP site.

    <img src=/w3timages/blueline.gif width=33% height=2>

    Another way to increase the speed is to insert <code>
    Application.ScreenUpdating = False</code>
    at the beginning of the code, and set <code>
    Application.ScreenUpdating = True</code>
    at the end, preferably in an "exit" section that is always executed even if an error occurred:

    Sub Something()
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    ...

    ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Best practice: Selection vs. Range (Word 2002 SP-2)

    When I first started programming in VBA, I really relied on the Selection object, because it was easy to understand, and many of its methods and properties were analogous to what I'd experienced in the Word interface. The Range object was Confusing and Scary to me, and so I tended to avoid it.

    But really the Selection object was a crutch. I looked at every problem from the perspective of trying to replicate manual actions with macro code. As an example, here's one (god-awful) way to bold the second word in the second paragraph in a document:

    <pre>Selection.Expand wdStory
    Selection.Collapse wdCollapseStart
    Selection.Expand wdParagraph
    Selection.Collapse wdCollapseEnd
    Selection.MoveEnd unit:=wdWord, Count:=2
    Selection.MoveEnd unit:=wdCharacter, Count:=-1
    Selection.MoveStart unit:=wdWord, Count:=1
    Selection.Range.Bold = True
    </pre>


    I'm guessing (and hoping) that I'm not the only one who started out writing macros like this. But at the time, it did what needed to be done using a vocabulary and framework I was comfortable with: I knew what a selection was, and I could understand about collapsing it, and moving it, and so forth. Most of my early macros were filled with code focused on getting the cursor to a specific place so I could do something to the text that was selected. What a waste!

    A more sensible way of accomplishing the same thing as that beast up there would be:
    <pre>ActiveDocument.Paragraphs(2).Range.Words(2).B old = True
    </pre>


    Of course, it is true that the same sort of optimization could be done using the Selection object. But without letting go of the crutch of the Selection object, I never would have bothered. For me, I never really understood the Word Object Model until I stopped using the Selection object. So in addition to the practical advantages, that's a less tangible, but arguably more important. benefit to the Range object. At least it was for me.

    While it may be that there's a negligible difference give the parameters you've specified, you'll eventually want to use that code again, and in a situation when there is a difference, and 99 times out of 100, that difference will favor using Range.

    As a rule of thumb, if your macro is limited to performing an action or set of actions at the insertion point (like inserting a picture) or on some selected text (like making text hidden), the Selection object is probably a reasonable choice.

    But there can be only one Selection object, and there can be multiple Range objects. That, coupled with the inherent speed advantage of the Range (with Hans' notable exception), make the Range object the better choice in almost every situation. In many situations, it will be the only choice.

    Just my $.02

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Best practice: Selection vs. Range (Word 2002 SP-2)

    I generally agree with all of the above comments.

    Selection-based code usually is easier to debug because you can watch what it's doing. Often it is not difficult then to convert it to Range-based code. (Bonus: when navigating tables using the "tab" key..., Selection.MoveRight wdCell, the selection includes only the contents and not the end-of-cell marker, but if the cell is empty, Selection.Text does return is the end of cell marker. Sigh.)

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Best practice: Selection vs. Range (Word 2002 SP-2)

    Thanks to everyone for the responses, which pretty much confirmed what I suspected.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Best practice: Selection vs. Range (Word 2002

    > As a rule of thumb, if your macro is limited to performing an action or set of actions at the insertion point

    I agree, up to a point.

    For me the defining moment is "Is the end-user involved"?

    The Select method is the end-user's only means of communicating with the computer - they click and they shift-arrow. That's how an end-user defines a chunk of text (or a cell, or a text box or ...).

    Once the end-user has Selected a chunk of text, us developers can convert that selection into a Range, and we can talk to the computer from the inside, with all the previously-noted advantages of Range.


    Since my end-user communicates with me (the developer) through a paramater-less subroutine procedure (a.k.a. macro), it follows that ONLY my macros should mention the selection object, and all other procedures should use the Range object. Yes, there are exceptions to this guideline, but I expect to have to justify them pretty welll before my peers.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Best practice: Selection vs. Range (Word 2002

    See also this post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •