Range and Selection Variables
Throughout this discussion, I will use rng
to denote a variable of type Range and sel
to denote a variable of type Selection. If you decide to type in the example code, don’t forget to declare these variables as:
Dim rng as Range Dim sel as Selection
I will also use the expression:
(rng or sel)
to denote either a Range variable or a Selection variable.
It is not uncommon for a program to use more than one Range object at the same time, so there will be more than one Range variable alive at the same time. On the other hand, since there can be only one Selection object per window pane, it is much less common to create multiple Selection variables.
However, we should at least consider one simple example of manipulating multiple selections. The following code first splits the active window into two panes. Then it selects the first paragraph in pane 1 and the second paragraph in pane 2. These are referred to by the Selection variables sel1
and sel2
. Finally, the selection in pane 2 is contracted by the number of characters that appear selected in paragraph 1.
ActiveWindow.Split = True ActiveWindow.Panes(1).Activate ActiveDocument.Paragraphs(1).Range.Select Set sel1 = Selection ActiveWindow.Panes(2).Activate ActiveDocument.Paragraphs(2).Range.Select Set sel2 = Selection sel2.MoveStart wdCharacter, sel1.Characters.Count
Figure 14-3 illustrates the effect of this code.
Incidentally, when working with more than one selection at a time, it is useful to know that the ...
Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.