Highlight that word in a sentence

D

Designingsally

i m trying to highlight a word "names" in a list.

I have three names:
Sally
Roger
Steve

The macros highlight the whole of "I have three names:" But i want only Name
to be highlighted. Is it possible?

macro i m tryin with is

Sub rs()

Dim lList As Long
Dim oRng As Range

For lList = ActiveDocument.Lists.Count To 1 Step -1
Set oRng = ActiveDocument.Lists(lList).ListParagraphs(1).Range

With oRng
If InStr(1, .Text,"names") Then
.Collapse Direction:=wdCollapseStart
.MoveStart Unit:=wdSentence, Count:=-1
.MoveEnd Unit:=wdCharacter, Count:=-1
If .Characters.Last <> ":" Then
.InsertAfter Text:=":"
End If

ActiveDocument.comments.Add Range:=oRng, Text:="AVOID"
End If
End With
Next lList


End Sub
 
P

Pesach Shelnitz

Hi Sally,

In your code you use Instr as if it returns True or False. However, Instr
returns an Integer that indicates the first position where the search string
is found within the text searched or 0 if the search string is not found. See
http://msdn.microsoft.com/en-us/library/8460tsh1.aspx for more details. Then,
you should use the value returned by Instr to help you select the word
"names".

The correct way to use Instr in your code would be as follows

Dim pos As Integer ' Add this line at the beginning of your macro

pos = InStr(1, .Text,"names", vbTextCompare)
If pos > 0 Then
.Start = .Start + pos
.End = .Start + Len("names")
ActiveDocument.comments.Add Range:=oRng, Text:="AVOID"
End If

Then you can extend the range and add the colon if necessary.
 
H

H. Druss

Designingsally said:
i m trying to highlight a word "names" in a list.

I have three names:
Sally
Roger
Steve

The macros highlight the whole of "I have three names:" But i want only
Name
to be highlighted. Is it possible?

macro i m tryin with is

Sub rs()

Dim lList As Long
Dim oRng As Range

For lList = ActiveDocument.Lists.Count To 1 Step -1
Set oRng = ActiveDocument.Lists(lList).ListParagraphs(1).Range

With oRng
If InStr(1, .Text,"names") Then
.Collapse Direction:=wdCollapseStart
.MoveStart Unit:=wdSentence, Count:=-1
.MoveEnd Unit:=wdCharacter, Count:=-1
If .Characters.Last <> ":" Then
.InsertAfter Text:=":"
End If

ActiveDocument.comments.Add Range:=oRng, Text:="AVOID"
End If
End With
Next lList


End Sub

Hi
This is one way.
=================================
Sub HighlightName()
Dim i As Long

Selection.WholeStory
i = InStr(Selection, "names")
Selection.Collapse

Dim o As Range
Set o = ActiveDocument.Range( _
Start:=ActiveDocument.Characters(i).Start, _
End:=ActiveDocument.Characters(i + 4).End)

o.HighlightColorIndex = wdYellow

End Sub
====================================

Good luck
Harold
 

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

Similar Threads


Top