expand to end of line

R

Robert H

I need to find a character then select to the end of the current line,
but not select anything in front of the character. Later I will make
a font change but I can work that out...

Im working with:

With Selection.Find
.Text = myCharacter
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute

Selection.Select

Selection.Expand Unit:=wdWord
Selection.Select

Im trying different things in the WdUnits items and using the expand
function but I am stumped at this point.

Any help will be appreciated

Robert
 
G

Greg Maxey

Robert,

Try:
Sub ScratchMacro()
Dim oRng As Word.Range
Dim myCharacter As String
Set oRng = ActiveDocument.Range
myCharacter = "a"
With oRng.Find
.Text = myCharacter
.Execute
If .Found Then
With oRng
.MoveEndUntil vbCr
'.MoveEnd wdCharacter, 1 'Unstet and include this line to include the
ending paragraph mark
.Select
End With
End If
End With
End Sub
 
H

Helmut Weber

Hi Robert,

this one will find the first "M" or "m" in
the docuemnt's main story
and select form it to the end of line:

Sub Test77709()
Dim rng As Range
Set rng = ActiveDocument.Range
With rng.Find
.Text = "M"
If .Execute Then
rng.End = rng.Bookmarks("\Line").End
rng.Select
End If
End With
End Sub

You may want to search for the next "M" in the line
the insertion point is in and, if found, select
to the rest of the line, which is a somewhat different story.


HTH nevertheless,

it is the rng.Bookmarks("\Line").End
which you can put to good use.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey

Helmut,

I didn't think of that, but when I checked your method in Word2007 I noticed
that it returns a runtime error if the cursor is not in a line that contains
an "M" when the code is run. I don't know if this happens in earlier
versions or not.
 
H

Helmut Weber

Hi Greg,
I didn't think of that, but when I checked your method in Word2007 I noticed
that it returns a runtime error if the cursor is not in a line that contains
an "M" when the code is run. I don't know if this happens in earlier
versions or not.

happens with 2002 as well.
An additional select will cure the problem,
which doesn't mean that I aöready understand what's going on.

Sub Test77709()
Dim rng As Range
Set rng = ActiveDocument.Range
With rng.Find
.Text = "M"
If .Execute Then
rng.Select ' <<<
rng.End = rng.Bookmarks("\Line").End
rng.Select
End If
End With
End Sub
 
G

Greg Maxey

Yes that works. I think I would use your method now over mine as it works
and mine doesn't. I only tested on a short sentence with a ending paragraph
mark :-(
 
R

Robert H

I was able to incorporate
rng.End = rng.Bookmarks("\Line").End
Into my code and it works great. thank, both of you for helping me
with this!

Robert
 

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