question for Helmut about returning line number of end of selection

L

Larry

Helmut, you had suggested this code for me, to determine if a selection
went to more than one line. At first it didn't work, then it did, as I
told you the other day. Now it's not working again. Even when the
extension runs to two lines, the msgbox says "on same line." I've
toggled to print view and page view, I've made sure that wrap to window
was off, and it's not in draft view.

With Selection.Characters
If .First.Information(10) <> .Last.Information(10) Then
MsgBox "not on same line"
Else
MsgBox "on same line"
End If
End With
 
L

Larry

To test it I did this.

This works.

MsgBox Selection.Information(wdFirstCharacterLineNumber)

But this, based on your code (which should be the same as the above),
returns -1.

MsgBox Selection.Characters.First.Information(10)

Yet the other day it was working.

Larry
 
L

Larry

Also, if wrap to window is true, then all three of these do NOT work,
they return -1.

MsgBox Selection.Information(wdFirstCharacterLineNumber)
MsgBox Selection.Information(10)
MsgBox Selection.Characters.First.Information(10)

But if wrap to window is false, then the first two work, but the third
returns -1:

MsgBox Selection.Information(wdFirstCharacterLineNumber)
MsgBox Selection.Information(10)
MsgBox Selection.Characters.First.Information(10)
 
H

Helmut Weber

Hi Larry,

all I can say is, that
Selection.Information(wdFirstCharacterLineNumber)
is nothing new at all and has been offerred as a solution
here in this group for many times by lots of people.

My code is a bit shorter than Greg's, but that's all.
Basically it is exactly the same.

I tested it in normal view and print view.

IMHO, it returns always -1 if the selection is in a textframe.
....Shapes.AddTextbox
....ShapeRange.TextFrame.TextRange.Select

WrapToWindow seems to have no effect on
information(wdFirstCharacterLineNumber)

Maybe somebody else knows better.
I seem to be at the and of my wisdom.

Why do you want to know at all, whether start
and end of the selection are in the same line?

Though I know, this is often required.

If information(wdFirstCharacterLineNumber)
turns out to be unreliable, then their are
workarounds. Which may have their bugs and
limitations as well.

Like this, which assumes, that the text in the line
at the start of the selection is different form the text
in the line at the end of the selection.

Dim rTmp As Range
Set rTmp = Selection.Range
Dim l1 As String
Dim l2 As String
With Selection
l1 = .Bookmarks("\line").Range.Text
Selection.Collapse direction:=wdCollapseEnd
l2 = .Bookmarks("\line").Range.Text
If l1 <> l2 Then
MsgBox "different line"
End If
End With
rTmp.Select

If that fails too, one could check the position
of selection.start vs. the position of selection.end
in terms of vertical distance form e.g. the top of the page.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Larry

Helmut, thanks for the extra work on this, I'll try that too. But I
found another way that seems stable and is not dependent on the
Information property which is just not dependable on my machine.

In my setup, RightEdge is the Selection.End plus 2. It's too
complicated to explain what I'm doing this for. I'll just show you how
I solved this particular problem within the larger macro.

If RightEdge >= ActiveDocument.Bookmarks("\Line").Range.End Then
n = RightEdge - ActiveDocument.Bookmarks("\Line").Range.End
' My new method that works. If RightEdge is more than two greater than
end of line,
' then I know that the selection extends to two lines, and I run code
just for
' that situation.
If Selection.Type = wdSelectionNormal And n > 2 Then
' Selection runs to more than one line.
 
L

Larry

Here's a simpler way of expressing the same idea:

If Selection.Type = wdSelectionNormal _
And Selection.End > ActiveDocument.Bookmarks("\Line").Range.End Then
MsgBox "more than one line"
End If

Larry
 
L

Larry

It can be even simpler:

If Selection.End > ActiveDocument.Bookmarks("\Line").Range.End Then _
MsgBox "more than one line"
 
L

Larry

I just realized why I (and maybe others) didn't think of this simple
solution earlier: Since the selection is on two lines, I think I
assumed that the line bookmark whose end number is being returned would
be the second line's bookmark, since the end of the bookmark range is
indeed on the second line. And if it were the second line's bookmark,
then this code wouldn't work. But as it turns out,
ActiveDocument.Bookmarks("\Line").Range.End is the bookmark of the line
on which the selection and the bookmark begin, not the line on which
they end. This made the solution possible, comparing the end of the
selection with the end of the line on which the selection starts. .

Larry
 
H

Helmut Weber

Pretty clever!

:)

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

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