Determining if a selection goes to a second line

L

Larry

How would you deterine if a multiword selection extends over more than
one line? In other words, a selection begins with a couple of words at
the end of one line, and continues to a couple of words on the next
line. I've been trying the Information property, but can't find any way
to make it work. The wdFirstCharacterColumnNumber it only gives you the
column number for the first character in the selection. Also, I haven't
been able to get wdFirstCharacterLineNumber to work at all.

Thanks,
Larry
 
G

Greg

Larry,

Just a stab, but try:
Sub Test()
Dim oRng As Range
Dim x As Long
Dim y As Long
y = Selection.Information(wdFirstCharacterLineNumber)

Set oRng = Selection.Range
oRng.Start = Selection.Range.End - 1
oRng.End = Selection.Range.End
x = oRng.Information(wdFirstCharacterLineNumber)
If x - y > 1 Then
MsgBox "The selection spans more than one line."
End If
End Sub
 
H

Helmut Weber

Hi Larry,
Also, I haven't been able to get
wdFirstCharacterLineNumber to work at all.

switch views beforehand,
from normal view to print layout and back
or the other way round, doesn't matter which way.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber

Hi Greg,

a happy new year to you, submariner!

I guess, you'll like that:

Sub Macro5()
' 10 = wdFirstCharacterLineNumber
With Selection.Characters
If .First.Information(10) <> _
.Last.Information(10) Then
MsgBox "not in same line"
End If
End With
End Sub

Plus the switching of views beforehand,
in a quick and dirty way, like:

With ActiveWindow.View
If .Type = wdNormalView Then
.Type = wdPrintView
.Type = wdNormalView
End If
If .Type = wdPrintView Then
.Type = wdNormalView
.Type = wdPrintView
End If
End With

By the way, once views have been switched once
in a working session for a doc, the problem,
that wdFirstCharacterLineNumber returns -1
has gone, IMHO.
--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Larry

Funny, I thought I replied yesterday, but the message isn't here.

Anyway, neither Helmut nor Greg's suggestion worked. There seems to be
a problem with using range with the Information property. It works with
Selection not Range. So I came up with this, which is awfully clunky.
I added a step which selects the range oRng, and then get its
Information, and then return the selection to the original selection:


Application.ScreenUpdating = False

Dim oRng As Range, r As Range
Dim x As Long
Dim y As Long
y = Selection.Information(wdFirstCharacterLineNumber)
Set r = Selection.Range
Set oRng = Selection.Range

oRng.Start = Selection.Range.End - 1
oRng.End = Selection.Range.End

' I had to add this line, actually selecting the range because this only
works
' with selections, not ranges.

oRng.Select
x = Selection.Information(wdFirstCharacterLineNumber)

'x = oRng.Information(wdFirstCharacterLineNumber)
'MsgBox y & " " & x

If x - y > 0 Then
r.Select
End If
 
H

Helmut Weber

Hi Larry,

forgive me, but for your question
multiword selection extends over more than one line
there were two working solutions.
It works with Selection not Range.

See above. ;-)

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Larry

Helmut,

Yesterday I tried this every way I could think of, and it didn't work.
Now it's working. Thanks.

Also, I got confused on the selection/range issue because ... well, it's
too complicated to explain.... :)


' 10 = wdFirstCharacterLineNumber
With Selection.Characters
If .First.Information(10) <> _
.Last.Information(10) Then
MsgBox "not in same line"
End If
End With
 

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