Selection.Range Unexpectedly Collapses

G

Greg Maxey

This came up in a separate topic and I thought it might be lost in the
weeds. I decided to bring it up here in a dedicated post in hope of
stimulating the discussion.

A group of us discovered quite by accident yesterday that if your delete
something that includes the start of a selection.range then the entire
selection is collapsed. If you delete just part of a selection.range or the
end of a selection.range then the selection is simply resized accordingly.

You can observe this by stepping through the following exercises and
observing the selection:

Sub Experiment1()
Dim oRng1 As Range
Dim oRng2 As Range
Set oRng1 = ActiveDocument.Range
Set oRng2 = ActiveDocument.Range
oRng1.Start = 10
oRng1.End = 20
oRng1.Select
oRng2.Start = 9
oRng2.End = 19
oRng2.Delete 'Selection collapses
End Sub

Sub Experiment2()
Dim oRng1 As Range
Dim oRng2 As Range
Set oRng1 = ActiveDocument.Range
Set oRng2 = ActiveDocument.Range
oRng1.Start = 10
oRng1.End = 20
oRng1.Select
oRng2.Start = 12
oRng2.End = 18
oRng2.Delete 'Selection resizes
End Sub

Sub Experiment3()
Dim oRng1 As Range
Dim oRng2 As Range
Set oRng1 = ActiveDocument.Range
Set oRng2 = ActiveDocument.Range
oRng1.Start = 10
oRng1.End = 20
oRng1.Select
oRng2.Start = 15
oRng2.End = 25
oRng2.Delete 'Selection resizes
End Sub

I am interested in what is happening under the hood in Word that causes the
complete collapse of the selection.range in exercise 1. Thanks
 
H

Helmut Weber

Hi Submariner,

the picture of a Word-document, I've made up for me,
is that of a 2-dimensional array,
left to right, like WordCharacter(x, y), where x is the character
and y all kind of information connected with character x.

According to the principle from left to right, it seems,
the crucial part of the information is in the second dimension
of the first character, like:

selection starts here
selection ends there

Still shorter to get the principle:

Selection.Characters.Last.Delete
Selection.Characters.First.Next.Delete
Selection.Characters.First.Delete


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Helmut,

Your experiment like your general code is so much more succinct than mine
;-)

Your explanation certainly sounds plausible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top