Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

highlight a line in word 1

Status
Not open for further replies.

RedBelly

Programmer
Mar 2, 2004
3
GB
Hi,
I'm new to vba and apologise if mail is simple one.
I have a word document containing e.g. the following text

beer is great
cider makes you ill

I want to create a macro which after finding the word beer will highlight the entire line containing this word. I have done the easy part. Can anyone help with the 'highlighting to end of the line'.

Sub beer()
'
' beer Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "beer"
.Replacement.Text = "beer"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = True
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ShowVisualBasicEditor = True
End Sub


Regards
RedBelly



 
Personally I would rather find money, but everyone to their taste (grin). Try this :-
'-------------------------------------------------------
Sub FindBeer()
Selection.find.Execute FindText:="beer", Forward:=True
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Range.HighlightColorIndex = wdYellow
End Sub
'--------------------------------------------------------





Regards
BrianB
Use CupOfCoffee to speed up all windows applications
================================
 
Hi BrainB
Thanks for your response.
How do you get this so it will highlight all lines containing "beer" in the entire document. At the moment the macro will just highlight the first line containing "Beer". Say for example if there was more than one line containing the word beer.
Regards
 
Here you go. It uses the predefined bookmark "Line" to set a range object, then highlights it. You may probably want to move the Selection point to the begining of the doc first - just to make the search simpler.

Sub HighLightAllBeer()
Dim rLine As Range
With ActiveDocument.Content.Find
.ClearFormatting
Do While .Execute(FindText:="beer", Forward:=True, _
Format:=True) = True
Selection.Find.Execute
Set rLine = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("\Line").Start, _
End:=ActiveDocument.Bookmarks("\Line").End)
rLine.HighlightColorIndex = wdYellow
Loop
End With
Set rLine = Nothing
End Sub

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top