A
Abeness
I'm confused about ranges and selections, and I know some of you can
straighten me out if you can spare a few minutes. I've got the following
code to surround currently selected text with markup:
Sub VP_HebrewFor10pt()
'apply HebrewFor10pt chartag
Dim MyRange As Range
Set MyRange = Selection.Range
MyRange.InsertBefore "<$[HebrewFor10pt>"
Selection.Collapse wdCollapseEnd
Selection.TypeText "<$]HebrewFor10pt>"
End Sub
Works perfectly for selected text:
<$[HebrewFor10pt>MyText<$]HebrewFor10pt>
The cursor is left at the end of the closing markup, and I'd expect.
However, I'd also like it to leave the cursor in between the markup when
no text is selected, i.e., just surround the insertion point with markup
so that the user can simply start typing inside the markup. I can do
this with a separate macro, but I'd like to combine the two.
So, I figured I'd count the characters in the range/selection to
distinguish between the insertion point with no text selected, and one
or more characters selected, but .Characters.Count returns "1" whether
no characters are selected or a single character is selected(!). Why?
What method or property could I use instead to distinguish these situations?
Curiously (to me at least), after collapsing to the start of the range
both Selection.TypeText and MyRange.InsertBefore *add* to the range:
MyRange.Select selects the inserted text PLUS the original range. If the
selection has not been collapsed, MyRange.InsertBefore adds to the
selection. Inserting text after the range, however, does *not* add to
the range.
Could someone please explain this apparently inconsistent behavior? Is
there a good detailed description of how the range object behaves under
various circumstances? I checked the MVP Word FAQ, but didn't find much
on ranges.
Tips and suggestions would be appreciated. Thanks,
Abe
straighten me out if you can spare a few minutes. I've got the following
code to surround currently selected text with markup:
Sub VP_HebrewFor10pt()
'apply HebrewFor10pt chartag
Dim MyRange As Range
Set MyRange = Selection.Range
MyRange.InsertBefore "<$[HebrewFor10pt>"
Selection.Collapse wdCollapseEnd
Selection.TypeText "<$]HebrewFor10pt>"
End Sub
Works perfectly for selected text:
<$[HebrewFor10pt>MyText<$]HebrewFor10pt>
The cursor is left at the end of the closing markup, and I'd expect.
However, I'd also like it to leave the cursor in between the markup when
no text is selected, i.e., just surround the insertion point with markup
so that the user can simply start typing inside the markup. I can do
this with a separate macro, but I'd like to combine the two.
So, I figured I'd count the characters in the range/selection to
distinguish between the insertion point with no text selected, and one
or more characters selected, but .Characters.Count returns "1" whether
no characters are selected or a single character is selected(!). Why?
What method or property could I use instead to distinguish these situations?
Curiously (to me at least), after collapsing to the start of the range
both Selection.TypeText and MyRange.InsertBefore *add* to the range:
MyRange.Select selects the inserted text PLUS the original range. If the
selection has not been collapsed, MyRange.InsertBefore adds to the
selection. Inserting text after the range, however, does *not* add to
the range.
Could someone please explain this apparently inconsistent behavior? Is
there a good detailed description of how the range object behaves under
various circumstances? I checked the MVP Word FAQ, but didn't find much
on ranges.
Tips and suggestions would be appreciated. Thanks,
Abe