Ambigious Comments

S

Sahana

I m trying to find the word "done" which appear as last word in every
list. THe macro functions well but displays two comment boxes instead
of one.
Can someone help me correct this code?

Try the data as
There are various forums as follows:
• Discussion and forums
• Main done
• Help


Code would be

Sub FindInLists()
Dim i As Long
Dim j As Long
Dim pos As Integer
Dim searchText As String
Dim myRange As Range


searchText = "done"
With ActiveDocument
For j = 1 To .Lists.Count
For i = 1 To .Lists(j).ListParagraphs.Count
Set myRange = .Lists(j).ListParagraphs(i).Range
pos = InStrRev(myRange.Text, searchText, -1, _
vbTextCompare)
If pos > Len(myRange.Text) - 6 Then
myRange.Start = myRange.Start + pos - 1
myRange.End = myRange.Start + Len(searchText) - 4
myRange.Select
Selection.comments.Add _
Range:=Selection.Range, Text:="Please use noun
or plural form"

End If


Next
Next
End With
End Sub
 
P

Pesach Shelnitz

Hi Sahana,

You can fix this problem by changing the line

If pos > Len(myRange.Text) - 6 Then

to

If pos > 0 And pos > Len(myRange.Text) - 6 Then

Also, to avoid some other problems, you should add the following line as the
last line of the macro before "End Sub"

Set myRange = Nothing
 

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