**** Woody's experiments with selections and ranges ****
I've been going around and around, trying to figure out why selections and
ranges are so weird in Word 97. Finally did a little experiment to nail things
down. You might want to replicate it.
Create a document with five paragraphs in it, like this:
Para 1
Para 2
Para 3
Para 4
Para 5
Then hop into VBA (Alt+F11) and Insert a Sub that looks like this:
Sub Blah ()
Dim rngTest As Range
Dim wrdDoc As Document
Set wrdDoc = ActiveDocument
Set rngTest = wrdDoc.Range _
(Start:=wrdDoc.Paragraphs(2).Range.Start, _
End:=wrdDoc.Paragraphs(4).Range.End)
rngTest.Select
' This is where the command goes
rngTest.Select
End Sub
With this code, rngTest is set to cover the three paragraphs, paras 2 thru 4,
including the para mark at the end of para 4. Then Word selects (highlights)
those three paragraphs, including the final para mark. Cool.
When I put each of these commands in the indicated spot, and run the macro one
line at a time (F8), I get all sorts of different results. Here are the
commands, and the results:
RngTest.InsertParagraph
Replaces the three paras with a single paragraph mark. RngTest changes to
include only that sole para mark. But the selection is reduced to an insertion
point before the new para mark.
RngTest.InsertParagraphBefore
Puts a new para mark in front of para number 2. Both the range and the
selection are extended to include the new para mark.
RngTest.InsertParagraphAfter
Puts a new para mark in front of para number 5. The range is extended to
include the new para mark, but the selection is *not* extended to include the
new para mark.
Selection.InsertParagraph
Replaces the selection with a single para mark (just as
rngTest.InsertParagraph). The new selection only includes that sole para mark.
But the range is reduced to an insertion point before the new para mark.
Selection.TypeParagraph
Puts a new para mark in front of para number 2 (just as
rngTest.InsertParagraphBefore). The selection becomes an insertion point after
the new paragraph mark (i.e., at the very beginning of para 2). The range is
extended to include the new para mark.
Selection.InsertParagraphBefore
Puts a new para mark in front of para number 2. Both the range and the
selection are extended to include the new para mark (just as
rngTest.InsertParagraphBefore).
Selection.InsertParagraphAfter
Puts a new para mark in front of para number 5. The selection is extended to
include the new para mark, but the range is *not* extended to include the new
para mark.
Selection.InsertBefore "some text"
Inserts text at the beginning of para 2. Extends both selection and range to
include the new text.
Selection.InsertAfter "some text"
Inserts text at the beginning of para 5. Extends the selection to include the
new text, but not the range.
Selecton.TypeText "some text"
Inserts text at the beginning of para 2. Turns selection into an insertion
point at the end of the inserted text. Extends range to include the text.
What's confusing about this? Aside from all the permutations and combinations,
these commands almost mimic the old WordBasic commands for Inserting text in a
document (analogous to Selection.InsertSomething) and adding text to bookmarks
(analogous to range.InsertSomething). They aren't exactly the same, though. In
WordBasic, text inserted at the beginning of a bookmark does not become part
of the bookmark. But it does become part of the bookmark with all of these
VBA/Word commands. In WordBasic, text inserted at the end of a bookmark *does*
become part of the bookmark. But in VBA/Word's Selection.InsertAfter and
Selection.InsertParagraphAfter, that doesn't hold true.
Some of the changes are very welcome: for example, in WordBasic, Insert used
to vary in functionality depending on whether Typing Replaces Selection was
turned on. In VBA/Word that isn't the case.
It's a brave new world out there.
Copyright © 1997 Woody Leonhard
| Migrating to Office97 |