Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

question for Helmut about returning line number of end of selection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Larry - 07 Jan 2006 18:33 GMT
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
Larry - 07 Jan 2006 19:37 GMT
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

> 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
[quoted text clipped - 10 lines]
> End If
> End With
Larry - 07 Jan 2006 20:12 GMT
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)

> To test it I did this.
>
[quoted text clipped - 28 lines]
> > End If
> > End With
Helmut Weber - 07 Jan 2006 23:21 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Larry - 08 Jan 2006 03:21 GMT
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.

> Hi Larry,
>
[quoted text clipped - 51 lines]
>
> HTH
Larry - 08 Jan 2006 04:21 GMT
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

> 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
[quoted text clipped - 69 lines]
> >
> > HTH
Larry - 08 Jan 2006 18:13 GMT
It can be even simpler:

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

> Here's a simpler way of expressing the same idea:
>
[quoted text clipped - 80 lines]
> > >
> > > HTH
Larry - 08 Jan 2006 19:27 GMT
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

> It can be even simpler:
>
[quoted text clipped - 86 lines]
> > > >
> > > > HTH
Helmut Weber - 08 Jan 2006 22:23 GMT
Pretty clever!

:-)

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.